*----------------------------------------------------------------------*
***INCLUDE ZFIU019_F02.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_DATA_QUERY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA_GR USING      "此form ZFIU021也会调用，调整参数时相应调整ZFIU021程序
      IV_PC_JSD_EXIST TYPE C    "排除结算单中存在的物料凭证
      IV_JSDDH_CURRENT TYPE ZFIT079_HEAD-JSDDH "当前单据结算单单号
      IV_WAERS TYPE ZFIT079_HEAD-WAERS "货币
      IT_R_BUKRS TYPE TT_R_BUKRS
      IT_R_LIFNR TYPE TT_R_LIFNR
      IT_R_BSART TYPE TT_R_BSART
      IT_R_EKGRP TYPE TT_R_EKGRP
      IT_R_EBELN TYPE TT_R_EBELN
      IT_R_EBELP TYPE TT_R_EBELP
      IT_R_EBENP TYPE TT_R_EBENP
      IT_R_WERKS TYPE TT_R_WERKS
      IT_R_MATNR TYPE TT_R_MATNR
      IT_R_GJAHR TYPE TT_R_GJAHR
      IT_R_BELNR TYPE TT_R_BELNR
      IT_R_CHARG TYPE TT_R_CHARG
      IT_R_ERNAM TYPE TT_R_ERNAM
      IT_R_AEDAT TYPE TT_R_AEDAT
      IV_VORGANG TYPE ZFIT079_HEAD-VORGANG
      IV_XWARE TYPE XWARE    "货款
      IV_XBNKYF TYPE XBNK    "运费
      IV_XBNKQT TYPE XBNK    " 其它费用
      IV_ZBL TYPE C          "保留收货数量金额为零的建议行
    CHANGING CT_DATA_GR TYPE TT_GR_QUERY_RESULT
             CS_MSG TYPE BAPIRET2.

  TYPES:
    BEGIN OF TY_TAX_DATA,
      MWSKZ TYPE EKPO-MWSKZ,
      ZFPSL TYPE ZFIT079_ITEM-ZFPSL,
    END OF TY_TAX_DATA.

  DATA:
    LV_INDEX           TYPE SY-TABIX,
    LV_TAXEXIST        TYPE C,
    LS_DATA_GR         LIKE LINE OF CT_DATA_GR,
    LT_TAX_DATA        TYPE TABLE OF TY_TAX_DATA WITH HEADER LINE,
    LS_MSG             TYPE BAPIRET2,
    LV_NO_VIM_PROPOSAL TYPE C,  "为空时标识需要用VIM 函数建议PO收货行
    LS_EKKO_EKPO       TYPE TY_EKKO_EKPO,
    LS_EKBE            TYPE TY_EKKO_EKPO,
    LT_EKKO_EKPO       TYPE STANDARD TABLE OF TY_EKKO_EKPO,
    LT_EKBE            TYPE STANDARD TABLE OF TY_EKKO_EKPO,
    LT_EKBE_TMP        TYPE STANDARD TABLE OF TY_EKKO_EKPO,
    LT_R_JSDDH         TYPE RANGE OF ZFIT079_HEAD-JSDDH WITH HEADER LINE,
    LT_R_JSDDH_EXIST   TYPE RANGE OF ZFIT079_HEAD-JSDDH WITH HEADER LINE.

  DATA:
    LS_DP_HEAD          TYPE /OPT/VIM_1HEAD,
    LV_DATE             TYPE SY-DATUM,
    LV_XBNK             TYPE XBNK,
    LV_TABIX            TYPE I,
    LV_JSD_EXIST_NOS    TYPE STRING,
    LS_R_EBELN          LIKE LINE OF IT_R_EBELN,
    LT_EBELN_EBELP      TYPE TABLE OF EKSEL WITH HEADER LINE,
    LT_PO_SEL           TYPE TABLE OF EKSEL WITH HEADER LINE,
    LT_VIM_PROPOSAL     TYPE TABLE OF /OPT/VIM_1RITEM_DATA WITH HEADER LINE,
    LT_VIM_PROPOSAL_TMP TYPE TABLE OF /OPT/VIM_1RITEM_DATA WITH HEADER LINE.

  REFRESH CT_DATA_GR[].
  CLEAR CS_MSG.

  "包含运费或其它费用
  IF IV_XBNKYF IS NOT INITIAL OR IV_XBNKQT IS NOT INITIAL.
    LV_XBNK = 'X'.
  ENDIF.

  IF IT_R_EBENP[] IS NOT INITIAL.
    LOOP AT IT_R_EBENP INTO DATA(LS_R_EBENP).
      CLEAR LT_EBELN_EBELP.
      SPLIT LS_R_EBENP-LOW AT '-' INTO LT_EBELN_EBELP-EBELN LT_EBELN_EBELP-EBELP.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          INPUT  = LT_EBELN_EBELP-EBELN
        IMPORTING
          OUTPUT = LT_EBELN_EBELP-EBELN.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          INPUT  = LT_EBELN_EBELP-EBELP
        IMPORTING
          OUTPUT = LT_EBELN_EBELP-EBELP.

      APPEND LT_EBELN_EBELP.

      CLEAR LS_R_EBELN.
      LS_R_EBELN-SIGN = 'I'.
      LS_R_EBELN-OPTION = 'EQ'.
      LS_R_EBELN-LOW = LT_EBELN_EBELP-EBELN.
      APPEND LS_R_EBELN TO IT_R_EBELN.
    ENDLOOP.
  ENDIF.

  SELECT
    EKBE~EBELN
    EKBE~EBELP
    EKBE~ZEKKN
    EKBE~VGABE
    EKBE~GJAHR
    EKBE~BELNR
    EKBE~BUZEI

    EKBE~BWART
    EKBE~LFGJA
    EKBE~LFBNR
    EKBE~LFPOS

    EKBE~MENGE
    EKBE~WRBTR
    EKBE~DMBTR
    EKBE~SHKZG
    EKBE~CPUDT  "输入日期
    EKBE~CPUTM  "输入时间
    EKBE~CHARG

    EKKO~BSART
    T161T~BATXT
    EKPO~KNTTP
    EKKO~EKORG
    EKKO~BUKRS
    T001~BUTXT
    EKKO~LIFNR
    LFA1~NAME1 AS LIFNR_NAME
    EKKO~BEDAT
    EKKO~AEDAT
    EKKO~ERNAM
    EKPO~WERKS
    T001W~NAME1 AS WERKS_NAME
    EKPO~MTART
    EKPO~MATNR
    MAKT~MAKTX
    EKPO~TXZ01
*    EKPO~MENGE
    EKPO~MEINS
    EKPO~LGORT
    T001L~LGOBE
    EKKO~WAERS
    EKPO~NETPR
    EKPO~PEINH
    EKPO~MWSKZ
    EKPO~WEBRE
    INTO CORRESPONDING FIELDS OF TABLE LT_EKKO_EKPO
    FROM EKKO
    JOIN EKPO
      ON EKKO~EBELN = EKPO~EBELN
    JOIN EKBE
      ON EKPO~EBELN = EKBE~EBELN
     AND EKPO~EBELP = EKBE~EBELP

    JOIN LFA1
      ON EKKO~LIFNR = LFA1~LIFNR

    LEFT JOIN T161T
      ON T161T~SPRAS = SY-LANGU
     AND EKKO~BSART = T161T~BSART
     AND T161T~BSTYP = 'F'  "采购订单

    LEFT JOIN MAKT
      ON EKPO~MATNR = MAKT~MATNR
     AND MAKT~SPRAS = SY-LANGU
    LEFT JOIN T001W
      ON EKPO~WERKS = T001W~WERKS
    LEFT JOIN T001L
      ON EKPO~WERKS = T001L~WERKS
     AND EKPO~LGORT = T001L~LGORT
    LEFT JOIN T001
     ON EKKO~BUKRS = T001~BUKRS
  WHERE EKKO~BUKRS IN IT_R_BUKRS
    AND EKKO~LIFNR IN IT_R_LIFNR
    AND EKKO~BSART IN IT_R_BSART
    AND EKKO~EKGRP IN IT_R_EKGRP
    AND EKPO~EBELN IN IT_R_EBELN
    AND EKPO~EBELP IN IT_R_EBELP
    AND EKPO~WERKS IN IT_R_WERKS
    AND EKPO~MATNR IN IT_R_MATNR
    AND EKKO~AEDAT IN IT_R_AEDAT
    AND EKPO~LOEKZ = SPACE        "没有删除标志
    "AND EKPO~WEBRE = 'X'         "基于收货的
    AND EKPO~REPOS = 'X'         "需要发票校验
    AND EKBE~VGABE IN ('1', '9') "收货/服务条目  '2', 'P'/发票/预制发票
    AND EKBE~GJAHR IN IT_R_GJAHR
    AND EKBE~BELNR IN IT_R_BELNR
    AND EKBE~CHARG IN IT_R_CHARG.
  "AND ( EKKO~FRGKE = 'Z' OR EKKO~BSART = 'ZP02' OR EKKO~ERNAM LIKE 'FF-MM%' )."已审批 或者 ZP02的单子 或者期初导入的数据
  IF SY-SUBRC <> 0.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = TEXT-M05.
    RETURN.
  ENDIF.

  "PO&ITEM 合并值有输入，先过滤结果值
  IF LT_EBELN_EBELP[] IS NOT INITIAL.
    SORT LT_EBELN_EBELP BY EBELN EBELP.
    LOOP AT LT_EKKO_EKPO INTO LS_EKKO_EKPO.
      LV_TABIX = SY-TABIX.

      READ TABLE LT_EBELN_EBELP TRANSPORTING NO FIELDS
        WITH KEY EBELN = LS_EKKO_EKPO-EBELN
                 EBELP = LS_EKKO_EKPO-EBELP
                 BINARY SEARCH.
      IF SY-SUBRC <> 0.
        DELETE LT_EKKO_EKPO INDEX LV_TABIX.
      ENDIF.
    ENDLOOP.

    IF LT_EKKO_EKPO[] IS INITIAL.
      CS_MSG-TYPE = 'E'.
      CS_MSG-MESSAGE = TEXT-M05.
      RETURN.
    ENDIF.
  ENDIF.

  "Start for 取物料凭证性能优化
  LT_EKBE_TMP = LT_EKKO_EKPO.
  "End

  LT_EKBE[] = LT_EKKO_EKPO.
  SORT LT_EKKO_EKPO BY EBELN EBELP.
  DELETE ADJACENT DUPLICATES FROM LT_EKKO_EKPO COMPARING EBELN EBELP.

  LOOP AT LT_EKKO_EKPO INTO LS_EKKO_EKPO.
    CLEAR LT_PO_SEL.
    LT_PO_SEL-EBELN = LS_EKKO_EKPO-EBELN.
    LT_PO_SEL-EBELP = LS_EKKO_EKPO-EBELP.
    APPEND LT_PO_SEL.
  ENDLOOP.

  "数据量比较多优化性能.
  IF LINES( LT_PO_SEL[] ) > 300.
    "服务类的采购订单不走该逻辑
    LOOP AT LT_EKKO_EKPO TRANSPORTING NO FIELDS WHERE BSART = 'Z13' OR BSART = 'Z2' OR BSART = 'Z4'.
      " OR WEBRE IS INITIAL. "不基于收货的发票校验
      EXIT.
    ENDLOOP.
    IF SY-SUBRC <> 0.

      IF IT_R_CHARG[] IS NOT INITIAL OR IT_R_BELNR[] IS NOT INITIAL. "只有批次或物料凭证录入时 才走该逻辑
        LV_NO_VIM_PROPOSAL = 'X'.

        "过滤冲销的、做过发票的物料凭证
        PERFORM FRM_FILTER_MAC_DOC USING IV_ZBL CHANGING LT_EKBE_TMP.
        LT_EKBE[] = LT_EKBE_TMP[].

        IF LT_EKBE[] IS INITIAL.
          CS_MSG-TYPE = 'E'.
          CS_MSG-MESSAGE = TEXT-M05.
          RETURN.
        ENDIF.

        SELECT
          EKBZ~EBELN,
          EKBZ~EBELP,
          EKBZ~STUNR,
          EKBZ~ZAEHK,
          EKBZ~VGABE,
          EKBZ~GJAHR,
          EKBZ~BELNR,
          EKBZ~BUZEI,
          EKBZ~MENGE,
          EKBZ~DMBTR,
          EKBZ~WRBTR,
          EKBZ~SHKZG,
          EKBZ~SHKKO, "用该字段判断 MIRO条件项的 借贷标识
          EKBZ~KSCHL,
          T685T~VTEXT
          INTO TABLE @DATA(LT_EKBZ)
          FROM EKBZ
          LEFT JOIN T685T
            ON EKBZ~KSCHL = T685T~KSCHL
           AND T685T~SPRAS = @SY-LANGU
           AND T685T~KVEWE = 'A'
           AND T685T~KAPPL = 'M'
           FOR ALL ENTRIES IN @LT_EKBE
         WHERE EBELN = @LT_EKBE-EBELN
           AND EBELP = @LT_EKBE-EBELP
           AND VGABE = @LT_EKBE-VGABE
           AND GJAHR = @LT_EKBE-GJAHR
           AND BELNR = @LT_EKBE-BELNR
           AND BUZEI = @LT_EKBE-BUZEI.

        LOOP AT LT_EKBE INTO LS_EKBE.
          CLEAR LT_VIM_PROPOSAL.
          LT_VIM_PROPOSAL-EBELN = LS_EKBE-EBELN.
          LT_VIM_PROPOSAL-EBELP = LS_EKBE-EBELP.
          LT_VIM_PROPOSAL-REF_DOC = LS_EKBE-BELNR.
          LT_VIM_PROPOSAL-REF_DOC_YEAR = LS_EKBE-GJAHR.
          LT_VIM_PROPOSAL-REF_DOC_IT = LS_EKBE-BUZEI.
          LT_VIM_PROPOSAL-MAKTX = LS_EKBE-MAKTX.
          LT_VIM_PROPOSAL-WRBTR = LS_EKBE-NETPR * LS_EKBE-MENGE.   "LS_EKBE-WRBTR.  因1月份PO创建条件项有问题，金额没有带到物料凭证
          LT_VIM_PROPOSAL-MENGE = LS_EKBE-MENGE.
          LT_VIM_PROPOSAL-BSTME = LS_EKBE-MEINS.
          LT_VIM_PROPOSAL-MWSKZ = LS_EKBE-MWSKZ.
          LT_VIM_PROPOSAL-SHKZG = LS_EKBE-SHKZG.

          IF IV_XWARE IS NOT INITIAL.
            APPEND LT_VIM_PROPOSAL.
          ENDIF.

          IF LV_XBNK IS NOT INITIAL. "运费或其它费用
            LOOP AT LT_EKBZ INTO DATA(LS_EKBZ) WHERE
              EBELN = LS_EKBE-EBELN AND
              EBELP = LS_EKBE-EBELP AND
              VGABE = LS_EKBE-VGABE AND
              GJAHR = LS_EKBE-GJAHR AND
              BELNR = LS_EKBE-BELNR AND
              BUZEI = LS_EKBE-BUZEI.

              CLEAR LT_VIM_PROPOSAL.
              LT_VIM_PROPOSAL-EBELN = LS_EKBZ-EBELN.
              LT_VIM_PROPOSAL-EBELP = LS_EKBZ-EBELP.
              LT_VIM_PROPOSAL-COND_TYPE = LS_EKBZ-KSCHL.
              LT_VIM_PROPOSAL-MAKTX = LS_EKBZ-VTEXT.
              LT_VIM_PROPOSAL-WRBTR = LS_EKBZ-WRBTR.
              LT_VIM_PROPOSAL-MENGE = LS_EKBZ-MENGE.
              IF LS_EKBZ-SHKKO = 'H'.
                LT_VIM_PROPOSAL-SHKZG = 'S'.
              ELSE.
                LT_VIM_PROPOSAL-SHKZG = 'H'.
              ENDIF.
              LT_VIM_PROPOSAL-BSTME = LS_EKBE-MEINS.

              CASE LS_EKBZ-KSCHL+0(2).
                  "   WHEN 'ZD'. "吊装费
                WHEN 'ZF'. "运费
                  IF IV_XBNKYF IS NOT INITIAL.
                    COLLECT LT_VIM_PROPOSAL.
                  ENDIF.

                WHEN OTHERS.
                  IF IV_XBNKQT IS NOT INITIAL.
                    COLLECT LT_VIM_PROPOSAL.
                  ENDIF.
              ENDCASE.
            ENDLOOP.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.

  IF LV_NO_VIM_PROPOSAL IS INITIAL.
    SELECT SINGLE
      *
      INTO @DATA(LS_MARV)
      FROM MARV
     WHERE BUKRS = 'CN10'.

    LV_DATE = LS_MARV-LFGJA && LS_MARV-LFMON && '01'.
    IF LV_DATE+0(6) < SY-DATUM+0(6). "当前账期未开
      LS_DP_HEAD-BUDAT = LV_DATE.
    ELSE.
      LS_DP_HEAD-BUDAT = SY-DATUM.
    ENDIF.

    LS_DP_HEAD-VORGANG = IV_VORGANG.
    LS_DP_HEAD-WAERS = IV_WAERS.

    CALL FUNCTION '/OPT/VIM_GET_PROPOSAL'
      EXPORTING
        HEAD              = LS_DP_HEAD
*       PO_CHECK          =
        XBNK              = LV_XBNK
        XWARE             = IV_XWARE
*       FROM_POST         =
      TABLES
        TAB_EKSEL         = LT_PO_SEL[]
        TAB_ITEMDATA      = LT_VIM_PROPOSAL[]
*       TAB_ACCOUNTINGDATA       =
*       TAB_ERROR         =
*       PO_LIST           =
*       BL_LIST           =
*       CON_LIST          =
*       TOR_LIST          =
      EXCEPTIONS
        NONE_OPEN         = 1
        INVALID_NUMBER    = 2
        MISSING_PARAMETER = 3
        CUSTOMER_EXIT     = 4
        OTHERS            = 5.
    IF SY-SUBRC <> 0.
      CS_MSG-TYPE = 'E'.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              INTO CS_MSG-MESSAGE
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      RETURN.
    ENDIF.

    "不基于收货的发票校验，批次有输入情况，更新数量、金额
    LOOP AT LT_EKKO_EKPO TRANSPORTING NO FIELDS WHERE WEBRE IS INITIAL. "不基于收货的发票校验
      EXIT.
    ENDLOOP.
    IF SY-SUBRC = 0.
      IF IT_R_CHARG[] IS NOT INITIAL . "只有批次录入时 才走该逻辑.
        SORT LT_EKBE BY EBELN EBELP.

        LOOP AT LT_VIM_PROPOSAL WHERE REF_DOC IS INITIAL AND COND_TYPE IS INITIAL.

          LOOP AT LT_EKBE INTO LS_EKBE WHERE EBELN = LT_VIM_PROPOSAL-EBELN AND EBELP = LT_VIM_PROPOSAL-EBELP
            AND WEBRE IS INITIAL. "不基于收货的发票校验.
            AT NEW EBELP.
              CLEAR:
               LT_VIM_PROPOSAL-MENGE,
               LT_VIM_PROPOSAL-WRBTR.
            ENDAT.

            IF LS_EKBE-SHKZG = 'H'.
              LT_VIM_PROPOSAL-MENGE = LT_VIM_PROPOSAL-MENGE - LS_EKBE-MENGE.
              LT_VIM_PROPOSAL-WRBTR = LT_VIM_PROPOSAL-WRBTR - LS_EKBE-WRBTR.
            ELSE.
              LT_VIM_PROPOSAL-MENGE = LT_VIM_PROPOSAL-MENGE + LS_EKBE-MENGE.
              LT_VIM_PROPOSAL-WRBTR = LT_VIM_PROPOSAL-WRBTR + LS_EKBE-WRBTR.
            ENDIF.
          ENDLOOP.

          MODIFY LT_VIM_PROPOSAL.
        ENDLOOP.

      ENDIF.
    ENDIF.
  ENDIF.

  "删除数量、金额为0的记录
  IF IV_ZBL IS INITIAL.
    DELETE LT_VIM_PROPOSAL WHERE MENGE IS INITIAL AND WRBTR IS INITIAL.
  ENDIF.

  IF IV_XBNKYF IS INITIAL. "排除运费
    DELETE LT_VIM_PROPOSAL WHERE COND_TYPE = 'ZF00'. "运费(值)
    DELETE LT_VIM_PROPOSAL WHERE COND_TYPE = 'ZF01'. "运费 %
    DELETE LT_VIM_PROPOSAL WHERE COND_TYPE = 'ZF02'. "运费/数量
  ENDIF.

  IF IV_XBNKQT IS INITIAL. "排除其它费用
    LOOP AT LT_VIM_PROPOSAL WHERE COND_TYPE IS NOT INITIAL.
      IF LT_VIM_PROPOSAL-COND_TYPE = 'ZF00' OR
        LT_VIM_PROPOSAL-COND_TYPE = 'ZF01' OR LT_VIM_PROPOSAL-COND_TYPE = 'ZF02'.
      ELSE.
        DELETE LT_VIM_PROPOSAL.
      ENDIF.
    ENDLOOP.
  ENDIF.

  "过滤不存在的物料凭证
  IF LV_NO_VIM_PROPOSAL IS INITIAL."VIM函数取的才需过滤
    SORT LT_EKBE BY EBELN EBELP VGABE GJAHR BELNR BUZEI.
    LOOP AT  LT_VIM_PROPOSAL WHERE COND_TYPE IS INITIAL.
      LV_TABIX = SY-TABIX.

      "填充批次时、过账日期
      IF LT_VIM_PROPOSAL-SHEET_NO IS NOT INITIAL.
        READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
          EBELN = LT_VIM_PROPOSAL-EBELN
          EBELP = LT_VIM_PROPOSAL-EBELP
          VGABE = '9'
          BELNR = LT_VIM_PROPOSAL-SHEET_NO
          BINARY SEARCH.
        IF SY-SUBRC <> 0.
          DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.
          CONTINUE.
        ENDIF.

      ELSEIF LT_VIM_PROPOSAL-REF_DOC IS NOT INITIAL.
        READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
                  EBELN = LT_VIM_PROPOSAL-EBELN
                  EBELP = LT_VIM_PROPOSAL-EBELP
                  VGABE = '1'
                  GJAHR = LT_VIM_PROPOSAL-REF_DOC_YEAR
                  BELNR = LT_VIM_PROPOSAL-REF_DOC
                  BUZEI = LT_VIM_PROPOSAL-REF_DOC_IT
                  BINARY SEARCH.
        IF SY-SUBRC <> 0.
          DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.
          CONTINUE.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.

  "检查是否有质检的物料凭证，若有过滤掉未质检完的物料凭证
  "NB 才有质检的情况
  LOOP AT LT_EKKO_EKPO TRANSPORTING NO FIELDS WHERE BSART = 'NB'.
    EXIT.
  ENDLOOP.

  IF SY-SUBRC = 0.
    DATA:
      LV_QM_QTY TYPE MSEG-MENGE.

    LT_VIM_PROPOSAL_TMP[] = LT_VIM_PROPOSAL[].
    DELETE LT_VIM_PROPOSAL_TMP WHERE REF_DOC IS INITIAL.
    IF LT_VIM_PROPOSAL_TMP[] IS NOT INITIAL.
      SELECT
        MBLNR,
        MJAHR,
        ZEILE,
        MATNR,
        INSMK,
        SGTXT
        INTO TABLE @DATA(LT_MSEG_CHECK)
        FROM MSEG
         FOR ALL ENTRIES IN @LT_VIM_PROPOSAL_TMP
       WHERE MBLNR = @LT_VIM_PROPOSAL_TMP-REF_DOC
         AND MJAHR = @LT_VIM_PROPOSAL_TMP-REF_DOC_YEAR
         AND ZEILE = @LT_VIM_PROPOSAL_TMP-REF_DOC_IT
         AND INSMK = 'X'. "质量检查

      IF LT_MSEG_CHECK[] IS NOT INITIAL.
        LOOP AT LT_MSEG_CHECK INTO DATA(LS_MSEG_CHECK).
          LS_MSEG_CHECK-SGTXT = LS_MSEG_CHECK-MBLNR && '/' && LS_MSEG_CHECK-ZEILE.
          MODIFY LT_MSEG_CHECK FROM LS_MSEG_CHECK.
        ENDLOOP.

        "取已经质检过的数量
        SELECT
          MBLNR,
          MJAHR,
          ZEILE,
          SHKZG,
          MATNR,
          MENGE,
          SGTXT
          INTO TABLE @DATA(LT_MSEG_CHECKED)
          FROM MSEG
           FOR ALL ENTRIES IN @LT_MSEG_CHECK
         WHERE SGTXT = @LT_MSEG_CHECK-SGTXT
           AND MATNR = @LT_MSEG_CHECK-MATNR
           AND XAUTO = @SPACE
           AND BWART IN ( '321', '322' ).

        SORT LT_MSEG_CHECK BY MBLNR MJAHR ZEILE.
        LOOP AT LT_VIM_PROPOSAL.
          LV_TABIX = SY-TABIX.

          READ TABLE LT_MSEG_CHECK INTO LS_MSEG_CHECK
            WITH KEY MBLNR = LT_VIM_PROPOSAL-REF_DOC
                     MJAHR = LT_VIM_PROPOSAL-REF_DOC_YEAR
                     ZEILE = LT_VIM_PROPOSAL-REF_DOC_IT
                     BINARY SEARCH.
          IF SY-SUBRC = 0.

            LV_QM_QTY = LT_VIM_PROPOSAL-MENGE.
            LOOP AT LT_MSEG_CHECKED INTO DATA(LS_MSEG_CHECKED)
              WHERE SGTXT = LS_MSEG_CHECK-SGTXT
                AND MATNR = LS_MSEG_CHECK-MATNR.

              IF LS_MSEG_CHECKED-SHKZG = 'H'.
                LV_QM_QTY = LV_QM_QTY - LS_MSEG_CHECKED-MENGE.
              ELSEIF LS_MSEG_CHECKED-SHKZG = 'S'.
                LV_QM_QTY = LV_QM_QTY + LS_MSEG_CHECKED-MENGE.
              ENDIF.
            ENDLOOP.

            "未质检完，删除该物料行
            IF LV_QM_QTY > 0 .
              DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.

              CS_MSG-MESSAGE = '已过滤未质检完的物料凭证&'(M41).
              REPLACE ALL OCCURRENCES OF '&' IN CS_MSG-MESSAGE WITH LS_MSEG_CHECK-SGTXT.
              CS_MSG-TYPE = 'W'.
            ENDIF.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.


  IF LT_VIM_PROPOSAL[] IS INITIAL.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = TEXT-M05.
    RETURN.
  ENDIF.

  "过滤结算单中存在的物料凭证/服务条目
  IF IV_VORGANG = '1'. "贷项凭证、后续借记、贷记无需过滤, 发票、贷项凭证需过滤
    IF IV_JSDDH_CURRENT IS NOT INITIAL.
      CLEAR LT_R_JSDDH.
      LT_R_JSDDH-SIGN = 'I'.
      LT_R_JSDDH-OPTION = 'EQ'.
      LT_R_JSDDH-LOW = IV_JSDDH_CURRENT.
      APPEND LT_R_JSDDH.
    ENDIF.

    LT_VIM_PROPOSAL_TMP[] = LT_VIM_PROPOSAL[].
    DELETE LT_VIM_PROPOSAL_TMP WHERE COND_TYPE IS NOT INITIAL.
    DELETE LT_VIM_PROPOSAL_TMP WHERE REF_DOC IS INITIAL AND SHEET_NO IS INITIAL.

    IF LT_VIM_PROPOSAL_TMP[] IS NOT INITIAL.
      "取物料凭证对应结算单数据
      SELECT B~*
        INTO TABLE @DATA(LT_JSD_EXIST)
        FROM ZFIT079_HEAD AS A
        JOIN ZFIT079_ITEM AS B
          ON A~JSDDH = B~JSDDH
         FOR ALL ENTRIES IN @LT_VIM_PROPOSAL_TMP
       WHERE A~ZYWLX <> @GC_ZYWLX_FOREIGN
         AND A~JSDZT <> @GC_JSDZT_DEPRECATED
         AND B~EBELN = @LT_VIM_PROPOSAL_TMP-EBELN
         AND B~EBELP = @LT_VIM_PROPOSAL_TMP-EBELP
         AND B~MBLNR = @LT_VIM_PROPOSAL_TMP-REF_DOC
         AND B~MJAHR = @LT_VIM_PROPOSAL_TMP-REF_DOC_YEAR
         AND B~ZEILE = @LT_VIM_PROPOSAL_TMP-REF_DOC_IT
         AND B~SHEET_NO = @LT_VIM_PROPOSAL_TMP-SHEET_NO
         AND B~COND_TYPE = @LT_VIM_PROPOSAL_TMP-COND_TYPE.

      IF LT_R_JSDDH[] IS NOT INITIAL."排除当前结算单
        DELETE LT_JSD_EXIST WHERE JSDDH IN LT_R_JSDDH.
      ENDIF.

      IF IV_PC_JSD_EXIST IS NOT INITIAL."排除结算单中存在的物料凭证/服务条目
        SORT LT_JSD_EXIST BY EBELN EBELP MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.
        SORT LT_EKBE BY EBELN EBELP VGABE GJAHR BELNR BUZEI.

        LOOP AT LT_VIM_PROPOSAL.
          LV_TABIX = SY-TABIX.
          READ TABLE LT_JSD_EXIST INTO DATA(LS_JSD_EXIST) WITH KEY
            EBELN = LT_VIM_PROPOSAL-EBELN
            EBELP = LT_VIM_PROPOSAL-EBELP
            MBLNR = LT_VIM_PROPOSAL-REF_DOC
            MJAHR = LT_VIM_PROPOSAL-REF_DOC_YEAR
            ZEILE = LT_VIM_PROPOSAL-REF_DOC_IT
            SHEET_NO  = LT_VIM_PROPOSAL-SHEET_NO
            COND_TYPE  = LT_VIM_PROPOSAL-COND_TYPE
            BINARY SEARCH.

          IF SY-SUBRC = 0.
            IF LT_VIM_PROPOSAL-SHEET_NO IS NOT INITIAL.

              "考虑服务条目部分行部分做发票的情况
              CLEAR LS_EKBE.
              READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
                     EBELN = LT_VIM_PROPOSAL-EBELN
                     EBELP = LT_VIM_PROPOSAL-EBELP
                     VGABE = '1'
                     LFBNR  = LT_VIM_PROPOSAL-SHEET_NO.
              IF SY-SUBRC = 0.
                IF ( LS_JSD_EXIST-WRBTR + LS_JSD_EXIST-BEZNK ) >= LS_EKBE-WRBTR."服务条目金额已经全部在结算单中
                  DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.

                  CLEAR LT_R_JSDDH_EXIST.
                  LT_R_JSDDH_EXIST-LOW = LS_JSD_EXIST-JSDDH.
                  APPEND LT_R_JSDDH_EXIST.
                ENDIF.
              ENDIF.

            ELSE. "考虑物料凭证部分行部分做发票的情况
              CLEAR LS_EKBE.
              READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
                     EBELN = LT_VIM_PROPOSAL-EBELN
                     EBELP = LT_VIM_PROPOSAL-EBELP
                     VGABE = '1'
                     GJAHR = LT_VIM_PROPOSAL-REF_DOC_YEAR
                     BELNR = LT_VIM_PROPOSAL-REF_DOC
                     BUZEI = LT_VIM_PROPOSAL-REF_DOC_IT
                     BINARY SEARCH.
              IF SY-SUBRC = 0.
                IF LS_JSD_EXIST-ERFMG = LS_EKBE-MENGE. "物料凭证数量已全部在结算单中存在
                  DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.

                  CLEAR LT_R_JSDDH_EXIST.
                  LT_R_JSDDH_EXIST-LOW = LS_JSD_EXIST-JSDDH.
                  APPEND LT_R_JSDDH_EXIST.
                ENDIF.
              ENDIF.
            ENDIF.
          ENDIF.
        ENDLOOP.

        IF LT_R_JSDDH_EXIST[] IS NOT INITIAL AND CS_MSG-TYPE <> 'E'.
          SORT LT_R_JSDDH_EXIST.
          DELETE ADJACENT DUPLICATES FROM LT_R_JSDDH_EXIST COMPARING ALL FIELDS.
          LOOP AT LT_R_JSDDH_EXIST.
            WRITE LT_R_JSDDH_EXIST-LOW TO LT_R_JSDDH_EXIST-LOW NO-ZERO.
            CONDENSE LT_R_JSDDH_EXIST-LOW.

            IF LV_JSD_EXIST_NOS IS INITIAL.
              LV_JSD_EXIST_NOS = LT_R_JSDDH_EXIST-LOW.
            ELSE.
              CONCATENATE LV_JSD_EXIST_NOS LT_R_JSDDH_EXIST-LOW INTO LV_JSD_EXIST_NOS SEPARATED BY ', '.
            ENDIF.
          ENDLOOP.

          IF LV_JSD_EXIST_NOS IS NOT INITIAL.
            CS_MSG-MESSAGE = '查询的PO收货行在结算单&中已存在'(M36).
            REPLACE ALL OCCURRENCES OF '&' IN CS_MSG-MESSAGE WITH LV_JSD_EXIST_NOS.
            CS_MSG-TYPE = 'W'.
          ENDIF.
        ENDIF.

        IF LT_VIM_PROPOSAL[] IS INITIAL..
          RETURN.
        ENDIF.

      ENDIF.
    ENDIF.
  ENDIF.

  IF LT_VIM_PROPOSAL[] IS INITIAL.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = TEXT-M05.
    RETURN.
  ENDIF.

  "包含运费或其它费用，费用数量要和物料数量一致
  DATA:
    LV_UPRICE TYPE ZFIT079_ITEM-ZFPSE,
    LV_MENGE  TYPE /OPT/VIM_1RITEM_DATA-MENGE.

  IF LV_XBNK IS NOT INITIAL AND LV_NO_VIM_PROPOSAL IS INITIAL.
    LT_VIM_PROPOSAL_TMP[] = LT_VIM_PROPOSAL[].
    LOOP AT LT_VIM_PROPOSAL WHERE COND_TYPE IS NOT INITIAL AND MENGE IS NOT INITIAL.
      LV_TABIX = SY-TABIX.

      CLEAR LV_MENGE.
      LOOP AT LT_VIM_PROPOSAL_TMP WHERE EBELN = LT_VIM_PROPOSAL-EBELN AND EBELP = LT_VIM_PROPOSAL-EBELP
        AND REF_DOC IS NOT INITIAL AND COND_TYPE IS INITIAL.

        LV_MENGE = LV_MENGE + LT_VIM_PROPOSAL_TMP-MENGE.
      ENDLOOP.

      IF LV_MENGE IS NOT INITIAL.
        LV_UPRICE = LT_VIM_PROPOSAL-WRBTR / LT_VIM_PROPOSAL-MENGE.
        IF LT_VIM_PROPOSAL-MENGE <> LV_MENGE.
          LT_VIM_PROPOSAL-MENGE = LV_MENGE.
          LT_VIM_PROPOSAL-WRBTR = LV_MENGE * LV_UPRICE.
        ENDIF.
*        CASE LT_VIM_PROPOSAL-COND_TYPE.
*          WHEN 'ZD02'. "吊装费/数量
*            LT_VIM_PROPOSAL-WRBTR = LV_MENGE * 10 / '1.06'.
*          WHEN OTHERS.
*            LT_VIM_PROPOSAL-WRBTR = LV_MENGE * LV_UPRICE.
*        ENDCASE.

        MODIFY LT_VIM_PROPOSAL.

      ELSE. "吊装费要和货款一起结算，没有货款则删除该行吊装费
        IF LT_VIM_PROPOSAL-COND_TYPE+0(2) = 'ZD'.
          DELETE LT_VIM_PROPOSAL INDEX LV_TABIX.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF LT_VIM_PROPOSAL[] IS INITIAL.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = TEXT-M05.
    RETURN.
  ENDIF.

  "编辑组合数据
  SORT LT_EKBE BY EBELN EBELP VGABE GJAHR BELNR BUZEI.
  SORT LT_JSD_EXIST BY MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.
  SORT LT_VIM_PROPOSAL BY EBELN EBELP REF_DOC_YEAR REF_DOC REF_DOC_IT SHEET_NO COND_TYPE LFSNR.

  LOOP AT LT_EKKO_EKPO INTO LS_EKKO_EKPO.
    CLEAR LS_DATA_GR.
    MOVE-CORRESPONDING LS_EKKO_EKPO TO LS_DATA_GR.
    IF LS_DATA_GR-TXZ01 IS INITIAL.
      LS_DATA_GR-TXZ01 = LS_EKKO_EKPO-MAKTX.
    ENDIF.

    IF LS_EKKO_EKPO-WEBRE IS INITIAL. "不基于收货的发票校验 删除批次信息
      CLEAR LS_DATA_GR-CHARG.
    ENDIF.

    "组合物料凭证信息
    LOOP AT  LT_VIM_PROPOSAL INTO DATA(LS_VIM_PROPOSAL) WHERE EBELN = LS_EKKO_EKPO-EBELN AND EBELP = LS_EKKO_EKPO-EBELP.
      IF LS_VIM_PROPOSAL-MWSKZ IS NOT INITIAL.
        LS_DATA_GR-MWSKZ = LS_VIM_PROPOSAL-MWSKZ.
      ELSE.

        IF LS_VIM_PROPOSAL-COND_TYPE IS NOT INITIAL.
          CASE LS_VIM_PROPOSAL-COND_TYPE+0(2).
            WHEN 'ZD'."吊装费
              LS_DATA_GR-MWSKZ = 'J5'."6%
            WHEN 'ZF'. "运费
              LS_DATA_GR-MWSKZ = 'JA'."9%
            WHEN OTHERS.
              CLEAR LS_DATA_GR-MWSKZ.
          ENDCASE.
        ENDIF.
      ENDIF.

      IF LS_DATA_GR-MWSKZ IS NOT INITIAL.
        READ TABLE LT_TAX_DATA WITH KEY MWSKZ = LS_DATA_GR-MWSKZ.
        IF SY-SUBRC = 0.
          LS_DATA_GR-ZFPSL = LT_TAX_DATA-ZFPSL.
        ELSE.
          "税率设置
          PERFORM FRM_GET_TAX_RATE USING 'CN' LS_DATA_GR-MWSKZ CHANGING LS_DATA_GR-ZFPSL LV_TAXEXIST.

          CLEAR LT_TAX_DATA.
          LT_TAX_DATA-MWSKZ = LS_DATA_GR-MWSKZ.
          LT_TAX_DATA-ZFPSL = LS_DATA_GR-ZFPSL.
          APPEND LT_TAX_DATA.
        ENDIF.
      ENDIF.

      LS_DATA_GR-MBLNR = LS_VIM_PROPOSAL-REF_DOC.
      LS_DATA_GR-MJAHR = LS_VIM_PROPOSAL-REF_DOC_YEAR.
      LS_DATA_GR-ZEILE = LS_VIM_PROPOSAL-REF_DOC_IT.
      LS_DATA_GR-SHEET_NO = LS_VIM_PROPOSAL-SHEET_NO.
      LS_DATA_GR-TXZ01 = LS_VIM_PROPOSAL-MAKTX.
      LS_DATA_GR-COND_TYPE = LS_VIM_PROPOSAL-COND_TYPE.
      LS_DATA_GR-LFSNR = LS_VIM_PROPOSAL-LFSNR.
      LS_DATA_GR-SHKZG = LS_VIM_PROPOSAL-SHKZG.

      LS_DATA_GR-WRBTR = LS_VIM_PROPOSAL-WRBTR.
      LS_DATA_GR-ERFMG = LS_VIM_PROPOSAL-MENGE.                       "到货数量
      LS_DATA_GR-ERFME = LS_VIM_PROPOSAL-BSTME.
      LS_DATA_GR-ZFPSE = LS_DATA_GR-WRBTR * LS_DATA_GR-ZFPSL / 100.

      "填充批次时、过账日期
      IF LS_DATA_GR-SHEET_NO IS NOT INITIAL.
        CLEAR LS_EKBE.
        READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
          EBELN = LS_DATA_GR-EBELN
          EBELP = LS_DATA_GR-EBELP
          VGABE = '9'
          BELNR = LS_DATA_GR-SHEET_NO
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          LS_DATA_GR-CHARG = LS_EKBE-CHARG.
          LS_DATA_GR-CPUDT = LS_EKBE-CPUDT.
          LS_DATA_GR-CPUTM = LS_EKBE-CPUTM.
        ELSE.
          CONTINUE.
        ENDIF.

      ELSEIF LS_DATA_GR-MBLNR IS NOT INITIAL.
        CLEAR LS_EKBE.
        READ TABLE LT_EKBE INTO LS_EKBE WITH KEY
                  EBELN = LS_DATA_GR-EBELN
                  EBELP = LS_DATA_GR-EBELP
                  VGABE = '1'
                  GJAHR = LS_DATA_GR-MJAHR
                  BELNR = LS_DATA_GR-MBLNR
                  BUZEI = LS_DATA_GR-ZEILE
                  BINARY SEARCH.
        IF SY-SUBRC = 0.
          LS_DATA_GR-CHARG = LS_EKBE-CHARG.
          LS_DATA_GR-CPUDT = LS_EKBE-CPUDT.
          LS_DATA_GR-CPUTM = LS_EKBE-CPUTM.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.

      IF IV_VORGANG = '1' OR IV_VORGANG = '2'. "后续借记、贷记不检查结算单是否存在
        CLEAR LS_JSD_EXIST.
        READ TABLE LT_JSD_EXIST INTO LS_JSD_EXIST WITH KEY
            EBELN = LS_VIM_PROPOSAL-EBELN
            EBELP = LS_VIM_PROPOSAL-EBELP
            MBLNR = LS_VIM_PROPOSAL-REF_DOC
            MJAHR = LS_VIM_PROPOSAL-REF_DOC_YEAR
            ZEILE = LS_VIM_PROPOSAL-REF_DOC_IT
            SHEET_NO  = LS_VIM_PROPOSAL-SHEET_NO
            COND_TYPE  = LS_VIM_PROPOSAL-COND_TYPE
            BINARY SEARCH.
        IF SY-SUBRC = 0 AND LS_JSD_EXIST-ERFMG = LS_EKBE-MENGE. "数量已全部在结算单中存在.
          LS_DATA_GR-JSDDH = LS_JSD_EXIST-JSDDH.
          LS_DATA_GR-JSDHH = LS_JSD_EXIST-JSDHH.
        ENDIF.
      ENDIF.

      IF IV_WAERS IS NOT INITIAL.
        LS_DATA_GR-WAERS = IV_WAERS.
      ENDIF.

      APPEND LS_DATA_GR TO CT_DATA_GR.

      CLEAR:
        LS_EKBE,
        LS_JSD_EXIST,
        LS_DATA_GR-JSDDH,
        LS_DATA_GR-JSDHH.
    ENDLOOP.
  ENDLOOP.

  SORT CT_DATA_GR BY EBELN EBELP MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_SAVE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_SAVE CHANGING CT_JSD_ITEM TYPE TT_ZFIT079_ITEM.
  DATA:
    LV_NUM         TYPE I,
    LS_ZFIT078_HIS TYPE ZFIT078_HIS,
    LT_ZFIT078_HIS TYPE STANDARD TABLE OF ZFIT078_HIS,
    LT_ITEM_DB_TMP TYPE TABLE OF ZFIT079_ITEM WITH HEADER LINE,
    LT_ITEM_DB     TYPE TABLE OF ZFIT079_ITEM WITH HEADER LINE.

  REFRESH CT_JSD_ITEM[].

  PERFORM FRM_DATA_CHECK.

  IF ZFIT079_HEAD-JSDDH IS INITIAL.
    PERFORM FRM_GENERATE_JSDDH USING ZFIT079_HEAD-ZYWLX
          CHANGING ZFIT079_HEAD-JSDDH.

    IF ZFIT079_HEAD-JSDDH IS INITIAL.
      MESSAGE '结算单号码生成失败，请检查配置'(M37) TYPE 'E'.
      RETURN.
    ENDIF.

    PERFORM FRM_LOCK_JSDDH USING ZFIT079_HEAD-JSDDH.

    ZFIT079_HEAD-CRNAM = SY-UNAME.
    ZFIT079_HEAD-CRDAT = SY-DATUM.
    ZFIT079_HEAD-CRTIM = SY-UZEIT.
  ELSE.
    ZFIT079_HEAD-CHNAM = SY-UNAME.
    ZFIT079_HEAD-CHDAT = SY-DATUM.
    ZFIT079_HEAD-CHTIM = SY-UZEIT.
  ENDIF.

  ZFIT079_HEAD-BKTXT = ZFIT079_HEAD-ZFPHM.

  LOOP AT GT_ALV WHERE SELECT IS NOT INITIAL.
    LV_NUM = LV_NUM + 1.
    CLEAR LT_ITEM_DB.
    MOVE-CORRESPONDING GT_ALV TO LT_ITEM_DB.
    LT_ITEM_DB-JSDDH = ZFIT079_HEAD-JSDDH.
    LT_ITEM_DB-JSDHH = LV_NUM.

    IF LT_ITEM_DB-ZYSFPHM IS INITIAL.
      LT_ITEM_DB-ZYSFPHM = ZFIT079_HEAD-ZFPHM.
    ENDIF.

    LT_ITEM_DB-EBELN = |{ GT_ALV-EBELN ALPHA = IN }|.
    LT_ITEM_DB-MATNR = |{ GT_ALV-MATNR ALPHA = IN }|.
    LT_ITEM_DB-CHARG = |{ GT_ALV-CHARG ALPHA = IN }|.
    LT_ITEM_DB-MBLNR = |{ GT_ALV-MBLNR ALPHA = IN }|.
    LT_ITEM_DB-SHEET_NO = |{ GT_ALV-SHEET_NO ALPHA = IN }|.
    LT_ITEM_DB-GL_HKONT = |{ GT_ALV-GL_HKONT ALPHA = IN }|.
    LT_ITEM_DB-GL_KOSTL = |{ GT_ALV-GL_KOSTL ALPHA = IN }|.

    IF ZFIT079_HEAD-VORGANG = '2' OR ZFIT079_HEAD-VORGANG = '4'."贷记
      LT_ITEM_DB-SHKZG = 'H'.
    ENDIF.

    APPEND LT_ITEM_DB.
  ENDLOOP.

  "只有1个PO号，自动填充抬头文本
  IF ZFIT079_HEAD-SGTXT IS INITIAL.
    LT_ITEM_DB_TMP[] = LT_ITEM_DB[].
    DELETE LT_ITEM_DB_TMP WHERE EBELN IS INITIAL.
    IF LT_ITEM_DB_TMP[] IS NOT INITIAL.
      SORT LT_ITEM_DB_TMP BY EBELN.
      DELETE ADJACENT DUPLICATES FROM LT_ITEM_DB_TMP COMPARING EBELN.

      IF LINES( LT_ITEM_DB_TMP ) = 1.
        LOOP AT LT_ITEM_DB_TMP.
          ZFIT079_HEAD-SGTXT = |收{ LT_ITEM_DB_TMP-EBELN }订单发票|.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.

  IF ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_CONSIGNMENT AND ZFIT079_HEAD-SGTXT IS INITIAL.
    MESSAGE '请录入凭证项目文本'(M33) TYPE 'E'.
    RETURN.
  ENDIF.

  IF ZFIT078_HEAD-INVOICE_LINE CS '17'.
    ZFIT079_HEAD-ZHSJE = ZFIT079_HEAD-ZHJSE + ZFIT079_HEAD-ZBHSJE_GX.
  ENDIF.

  "保存发票处理历史
  IF LT_ITEM_DB[] IS NOT INITIAL.
    SELECT *
      INTO TABLE @DATA(LT_ZFIT078_HIS_EXIST)
      FROM ZFIT078_HIS
       FOR ALL ENTRIES IN @LT_ITEM_DB
     WHERE ZFPHM = @LT_ITEM_DB-ZYSFPHM
       AND JSDDH = @LT_ITEM_DB-JSDDH
       AND DOCID = @ZFIT079_HEAD-DOCID
       AND EDI_DOCNUM = @ZFIT079_HEAD-DOCID
       AND BELNR = @ZFIT079_HEAD-BELNR
       AND GJAHR = @ZFIT079_HEAD-GJAHR.

    LOOP AT LT_ITEM_DB.
      READ TABLE LT_ZFIT078_HIS_EXIST TRANSPORTING NO FIELDS
        WITH KEY ZFPHM = LT_ITEM_DB-ZYSFPHM
                 JSDDH = LT_ITEM_DB-JSDDH
                 DOCID = ZFIT079_HEAD-DOCID
                 EDI_DOCNUM = ZFIT079_HEAD-DOCID
                 BELNR = ZFIT079_HEAD-BELNR
                 GJAHR = ZFIT079_HEAD-GJAHR.
      IF SY-SUBRC <> 0."不存在更新发票处理历史
        CLEAR LS_ZFIT078_HIS.
        LS_ZFIT078_HIS-ZFPHM = LT_ITEM_DB-ZYSFPHM.
        LS_ZFIT078_HIS-CHNAM = SY-UNAME.
        LS_ZFIT078_HIS-CHDAT = SY-DATUM.
        LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
        LS_ZFIT078_HIS-ZFPZT = '20'.  "发票结算单处理中
        LS_ZFIT078_HIS-JSDDH = LT_ITEM_DB-JSDDH.
        LS_ZFIT078_HIS-EDI_DOCNUM = ZFIT079_HEAD-DOCID.
        LS_ZFIT078_HIS-DOCID = ZFIT079_HEAD-DOCID.
        LS_ZFIT078_HIS-BELNR = ZFIT079_HEAD-BELNR.
        LS_ZFIT078_HIS-GJAHR = ZFIT079_HEAD-GJAHR.
        APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
      ENDIF.
    ENDLOOP.

    SORT LT_ZFIT078_HIS.
    DELETE ADJACENT DUPLICATES FROM LT_ZFIT078_HIS COMPARING ALL FIELDS.
  ENDIF.

  CT_JSD_ITEM[] = LT_ITEM_DB[].

  DELETE FROM ZFIT079_ITEM WHERE JSDDH = ZFIT079_HEAD-JSDDH.

  MODIFY ZFIT079_HEAD FROM ZFIT079_HEAD.
  MODIFY ZFIT079_ITEM FROM TABLE LT_ITEM_DB.

  IF LT_ZFIT078_HIS[] IS NOT INITIAL.
    MODIFY ZFIT078_HIS FROM TABLE LT_ZFIT078_HIS.
  ENDIF.

  COMMIT WORK AND WAIT.

  MESSAGE TEXT-M10 TYPE 'S'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_SUBMIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_SUBMIT .
  DATA:
    LV_STR        TYPE STRING,
    LV_STR_TMP    TYPE STRING,
    LS_MSG        TYPE BAPIRET2,
    LT_JSD_ITEM   TYPE TT_ZFIT079_ITEM,
    LV_AMT_DIFFER TYPE ZFIT079_HEAD-ZBHSJE.

  PERFORM FRM_DATA_CHECK.

  PERFORM FRM_DATA_SAVE CHANGING LT_JSD_ITEM.

  "附件信息获取/显示
  PERFORM FRM_SYNCFILE USING ZFIT079_HEAD SPACE.

  PERFORM FRM_ATTACH_CHECK.

  "弹出框提示信息
  LV_AMT_DIFFER = ABS( ZFIT079_HEAD-ZHSJE ) - ABS( ZFIT079_HEAD-ZHJSE_GX ) - ABS( ZFIT079_HEAD-ZBHSJE_GX ).
  IF LV_AMT_DIFFER IS NOT INITIAL.
    LV_STR_TMP = LV_AMT_DIFFER.
    CONDENSE LV_STR_TMP.
    LV_STR = TEXT-M00.
    REPLACE ALL OCCURRENCES OF '&' IN LV_STR WITH LV_STR_TMP.
  ENDIF.

  GV_POPUP_MSG = LV_STR && TEXT-M01.
  CONDENSE GV_POPUP_MSG.

  CLEAR GV_POPUP_YES.
  PERFORM FRM_POPUP_TO_CONFIRM USING GV_POPUP_MSG CHANGING GV_POPUP_YES.

  IF GV_POPUP_YES IS NOT INITIAL.
    IF ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_CONSIGNMENT.
      PERFORM FRM_IDOC_TRIGGER USING ZFIT078_HEAD CHANGING ZFIT079_HEAD LS_MSG.
    ELSE.
      PERFORM FRM_MRKO_POST USING LT_JSD_ITEM CHANGING ZFIT079_HEAD LS_MSG.
    ENDIF.

    MESSAGE LS_MSG-MESSAGE TYPE LS_MSG-TYPE.
    IF LS_MSG-TYPE = 'S'.
      GV_MODE = GC_MODE_DISPLAY.

      PERFORM FRM_UNLOCK_JSDDH USING ZFIT079_HEAD-JSDDH.
    ENDIF.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_VOID
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_VOID .
  DATA:
    LS_ZFIT078_HIS  TYPE ZFIT078_HIS,
    LT_ZFIT078_HIS  TYPE STANDARD TABLE OF ZFIT078_HIS,
    LT_ZFIT079_ITEM TYPE TABLE OF ZFIT079_ITEM WITH HEADER LINE.

  IF ZFIT079_HEAD-JSDDH IS INITIAL.
    MESSAGE TEXT-M03 TYPE 'E'.
    RETURN.
  ENDIF.

  IF ZFIT079_HEAD-JSDZT <> GC_JSDZT_DRAFT."草稿
    MESSAGE TEXT-M04 TYPE 'E'.
    RETURN.
  ENDIF.

  GV_POPUP_MSG = TEXT-M02.

  CLEAR GV_POPUP_YES.
  PERFORM FRM_POPUP_TO_CONFIRM USING GV_POPUP_MSG CHANGING GV_POPUP_YES.

  IF GV_POPUP_YES IS NOT INITIAL.

    ZFIT079_HEAD-JSDZT = GC_JSDZT_DEPRECATED.
    ZFIT079_HEAD-CHNAM = SY-UNAME.
    ZFIT079_HEAD-CHDAT = SY-DATUM.
    ZFIT079_HEAD-CHTIM = SY-UZEIT.

    SELECT *
      INTO TABLE LT_ZFIT079_ITEM
      FROM ZFIT079_ITEM
     WHERE JSDDH = ZFIT079_HEAD-JSDDH.

    "保存发票处理历史
    IF LT_ZFIT079_ITEM[] IS NOT INITIAL.
      LOOP AT LT_ZFIT079_ITEM.
        CLEAR LS_ZFIT078_HIS.
        LS_ZFIT078_HIS-ZFPHM = LT_ZFIT079_ITEM-ZYSFPHM.
        LS_ZFIT078_HIS-CHNAM = SY-UNAME.
        LS_ZFIT078_HIS-CHDAT = SY-DATUM.
        LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
        LS_ZFIT078_HIS-ZFPZT = '10'.  "已接受
        LS_ZFIT078_HIS-JSDDH = LT_ZFIT079_ITEM-JSDDH.
        LS_ZFIT078_HIS-EDI_DOCNUM = ZFIT079_HEAD-EDI_DOCNUM.
        LS_ZFIT078_HIS-DOCID = ZFIT079_HEAD-DOCID.
        LS_ZFIT078_HIS-BELNR = ZFIT079_HEAD-BELNR.
        LS_ZFIT078_HIS-GJAHR = ZFIT079_HEAD-GJAHR.
        LS_ZFIT078_HIS-MSG = '发票结算单作废'(T04).
        APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
      ENDLOOP.

      SORT LT_ZFIT078_HIS.
      DELETE ADJACENT DUPLICATES FROM LT_ZFIT078_HIS COMPARING ALL FIELDS.
    ENDIF.

    MODIFY ZFIT079_HEAD FROM ZFIT079_HEAD.

    IF LT_ZFIT078_HIS[] IS NOT INITIAL.
      MODIFY ZFIT078_HIS FROM TABLE LT_ZFIT078_HIS.
    ENDIF.

    COMMIT WORK AND WAIT.

    GV_MODE = GC_MODE_DISPLAY.
    MESSAGE TEXT-M13 TYPE 'S'.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CACULATE_ITEM
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_CACULATE_ITEM .
  DATA:
    LV_ZFPSE TYPE ZFIT079_ITEM-ZFPSE.

  CLEAR:
   ZFIT079_HEAD-ZHJSE_GX,
   ZFIT079_HEAD-ZBHSJE_GX,
   ZFIT079_HEAD-ZHJSE_CY,
   ZFIT079_HEAD-ZBHSJE_CY.

  LOOP AT GT_ALV WHERE SELECT IS NOT INITIAL.
    IF GT_ALV-GL_SHKZG = 'H' OR GT_ALV-SHKZG = 'H'.
      LV_ZFPSE = LV_ZFPSE - GT_ALV-ZFPSE - GT_ALV-GL_ZFPSE.
      ZFIT079_HEAD-ZBHSJE_GX = ZFIT079_HEAD-ZBHSJE_GX - GT_ALV-WRBTR - GT_ALV-BEZNK - GT_ALV-GL_WRBTR.
    ELSE.
      LV_ZFPSE = LV_ZFPSE + GT_ALV-ZFPSE + GT_ALV-GL_ZFPSE.
      ZFIT079_HEAD-ZBHSJE_GX = ZFIT079_HEAD-ZBHSJE_GX + GT_ALV-WRBTR + GT_ALV-BEZNK + GT_ALV-GL_WRBTR.
    ENDIF.

  ENDLOOP.

  ZFIT079_HEAD-ZHJSE_GX = LV_ZFPSE.

  ZFIT079_HEAD-ZBHSJE_CY = ABS( ZFIT079_HEAD-ZBHSJE ) - ABS( ZFIT079_HEAD-ZBHSJE_GX ).
  ZFIT079_HEAD-ZHJSE_CY  = ABS( ZFIT079_HEAD-ZHJSE ) - ABS( ZFIT079_HEAD-ZHJSE_GX ).

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_CHECK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_CHECK .
  DATA:
    LV_ERROR_MSG    TYPE STRING,
    LS_ZFIT079_HEAD TYPE ZFIT079_HEAD.

  IF ZFIT079_HEAD-ZFPHM IS INITIAL.
    MESSAGE TEXT-M06 TYPE 'E'.
    RETURN.
  ENDIF.

  "放后面data save里检查了
*  IF ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_CONSIGNMENT AND ZFIT079_HEAD-SGTXT IS INITIAL.
*    MESSAGE '请录入凭证项目文本'(M33) TYPE 'E'.
*    RETURN.
*  ENDIF.

  "检查其它发票结算单中该发票是否已经存在
  SELECT SINGLE *
    FROM ZFIT079_HEAD
    INTO LS_ZFIT079_HEAD
   WHERE ZFPHM = ZFIT079_HEAD-ZFPHM
     AND ZYWLX <> GC_ZYWLX_FOREIGN
     AND JSDZT <> GC_JSDZT_DEPRECATED
     AND JSDDH <> ZFIT079_HEAD-JSDDH.
  IF SY-SUBRC = 0.
    LV_ERROR_MSG = TEXT-M19.
    REPLACE ALL OCCURRENCES OF '&' IN LV_ERROR_MSG WITH LS_ZFIT079_HEAD-JSDDH.
    MESSAGE LV_ERROR_MSG TYPE 'E'.
    RETURN.
  ENDIF.

  "公司代码和供应商代码不能为空
  IF ZFIT079_HEAD-BUKRS IS INITIAL OR ZFIT079_HEAD-LIFNR IS INITIAL.
    MESSAGE TEXT-M07 TYPE 'E'.
    RETURN.
  ENDIF.

  "公司代码是否存在
  SELECT SINGLE
    BUTXT
    INTO ZFIT079_HEAD-ZGFMC
    FROM T001
   WHERE BUKRS = ZFIT079_HEAD-BUKRS.
  IF SY-SUBRC <> 0.
    LV_ERROR_MSG = TEXT-M23.
    REPLACE ALL OCCURRENCES OF '&' IN LV_ERROR_MSG WITH ZFIT079_HEAD-BUKRS.
    MESSAGE LV_ERROR_MSG TYPE 'E'.
    RETURN.
  ENDIF.

  "供应商是否存在
  SELECT SINGLE
    NAME1
    INTO ZFIT079_HEAD-ZXFMC
    FROM LFA1
   WHERE LIFNR = ZFIT079_HEAD-LIFNR.
  IF SY-SUBRC <> 0.
    LV_ERROR_MSG = TEXT-M24.
    REPLACE ALL OCCURRENCES OF '&' IN LV_ERROR_MSG WITH ZFIT079_HEAD-LIFNR.
    MESSAGE LV_ERROR_MSG TYPE 'E'.
    RETURN.
  ENDIF.

  IF ZFIT078_HEAD-INVOICE_LINE NS '17'. "海关缴款书
    "票面信息必填
    IF ZFIT079_HEAD-ZHSJE IS INITIAL OR ZFIT079_HEAD-ZBHSJE IS INITIAL OR ZFIT079_HEAD-ZKPSJ IS INITIAL
      OR ZFIT079_HEAD-WAERS IS INITIAL.
      MESSAGE TEXT-M18 TYPE 'E'.
      RETURN.
    ENDIF.

    "票面金额检查
    IF ZFIT079_HEAD-ZHSJE <> ( ZFIT079_HEAD-ZBHSJE + ZFIT079_HEAD-ZHJSE ).
      MESSAGE TEXT-M25 TYPE 'E'.
      RETURN.
    ENDIF.
  ENDIF.

  "行项目必须选择一条
  LOOP AT GT_ALV WHERE SELECT IS NOT INITIAL.
    IF ZFIT079_HEAD-JSDLX = GC_JSDLX_PO AND GT_ALV-EBELN IS NOT INITIAL.

      IF ZFIT078_HEAD-INVOICE_LINE CS '17' OR "海关缴款书
          GT_ALV-COND_TYPE+0(2) = 'ZF'.  "运费
      ELSE.
        IF GT_ALV-LIFNR <> ZFIT079_HEAD-LIFNR OR GT_ALV-BUKRS <> ZFIT079_HEAD-BUKRS.
          LV_ERROR_MSG = TEXT-M17.
          EXIT.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
  IF SY-SUBRC <> 0.
    MESSAGE TEXT-M08 TYPE 'E'.
    RETURN.

  ELSE.
    IF LV_ERROR_MSG IS NOT INITIAL.
      MESSAGE LV_ERROR_MSG TYPE 'E'.
      RETURN.
    ENDIF.

  ENDIF.

  "寄售必须选择物料凭证下的所有行项目
  DATA:
    LT_ALV TYPE TABLE OF TY_GR_QUERY_RESULT WITH HEADER LINE..
  IF ZFIT079_HEAD-ZYWLX = GC_ZYWLX_CONSIGNMENT.
    LT_ALV[] = GT_ALV[].

    LOOP AT GT_ALV WHERE SELECT IS NOT INITIAL.
      LOOP AT LT_ALV WHERE MBLNR = GT_ALV-MBLNR AND MJAHR = GT_ALV-MJAHR AND SELECT IS INITIAL.

        MESSAGE TEXT-M40 TYPE 'E'.
        EXIT.
      ENDLOOP.

      IF SY-SUBRC = 0.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FPJSD_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GV_JSDDH
*&---------------------------------------------------------------------*
FORM FRM_GET_FPJSD_DATA  USING IV_JSDDH TYPE ZFIT079_HEAD-JSDDH.

  SELECT SINGLE *
    INTO ZFIT079_HEAD
    FROM ZFIT079_HEAD
   WHERE JSDDH = IV_JSDDH.
  IF SY-SUBRC <> 0.
    MESSAGE TEXT-M09 TYPE 'E'.
    RETURN.
  ENDIF.

  SELECT SINGLE *
    INTO ZFIT078_HEAD
    FROM ZFIT078_HEAD
   WHERE ZFPHM = ZFIT079_HEAD-ZFPHM.

  IF ZFIT079_HEAD-JSDZT <> GC_JSDZT_DRAFT. "非草稿状态
    GV_MODE = GC_MODE_DISPLAY.
  ENDIF.

  PERFORM FRM_GET_FPJSD_ITEM_DATA.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_IDOC_TRIGGER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_IDOC_TRIGGER    "该Form ZFIU021程序也会调用
    USING IS_INV_HEAD TYPE ZFIT078_HEAD
   CHANGING CS_JSD_HEAD TYPE ZFIT079_HEAD
            CS_MSG TYPE BAPIRET2.

  DATA:
    LS_EDI_CTL  TYPE EDIDC,
    LS_EDI_DATA TYPE EDIDD,
    LT_EDI_DATA TYPE STANDARD TABLE OF EDIDD,
    LV_DOCID    TYPE /OPT/DOCID,
    LV_LIFNR    TYPE KNA1-LIFNR,
    LV_BUKRS    TYPE T001-BUKRS,
    LV_BUTXT    TYPE T001-BUTXT,
    LV_NAME1    TYPE LFA1-NAME1,
    LV_DOCNUM   TYPE EDIDC-DOCNUM,
    LV_KBETR    TYPE KONP-KBETR,
    LV_ERROR    TYPE EDI_HELP-ERROR_FLAG,
    LV_MSG      TYPE BAPIRET2-MESSAGE.

  DATA:
    LS_E1EDK01 TYPE    E1EDK01,
    LS_E1EDKA1 TYPE    E1EDKA1,
    LS_E1EDK02 TYPE    E1EDK02,
    LS_E1EDK03 TYPE    E1EDK03,
    LS_E1EDK05 TYPE    E1EDK05,
    LS_E1EDK04 TYPE    E1EDK04,
    LS_E1EDK17 TYPE    E1EDK17,
    LS_E1EDK18 TYPE    E1EDK18,
    LS_E1EDK23 TYPE    E1EDK23,
    LS_E1EDK28 TYPE    E1EDK28,
    LS_E1EDK29 TYPE    E1EDK29,
    LS_E1EDK14 TYPE    E1EDK14,
    LS_E1EDS01 TYPE    E1EDS01.

  "CONDENSE IV_ACHIVE_ID.

  LS_E1EDK01-ABLAD = CS_JSD_HEAD-BKTXT.
  LS_E1EDK01-CURCY = CS_JSD_HEAD-WAERS.

  LS_E1EDK01-KUNDEUINR = IS_INV_HEAD-ZGFSH.
  LS_E1EDK01-EIGENUINR = IS_INV_HEAD-ZXFSH.
  "LS_E1EDK01-ABLAD = IV_INVOICE_CODE.
*  LS_E1EDK01-ZTERM = IV_USER_ID.
*  LS_E1EDK01-AUGRU_BEZ = IV_ACHIVE_ID+3.

*  IF IV_TOTAL_AMT < 0. "红字发票
*    LS_E1EDK01-HWAER = '4'. "发票事务 后续贷记
*  ENDIF.
  LS_E1EDK01-HWAER = CS_JSD_HEAD-VORGANG.

  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDK01'.
  LS_EDI_DATA-SDATA = LS_E1EDK01.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.

  LS_E1EDKA1-PARVW = 'LF'.
  LS_E1EDKA1-LIFNR = CS_JSD_HEAD-LIFNR.
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDKA1'.
  LS_EDI_DATA-SDATA = LS_E1EDKA1.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.

*  "项目文本
*  LS_E1EDK17-QUALF = '001'.
*  LS_E1EDK17-LKTEXT = CS_JSD_HEAD-SGTXT.
*  CLEAR LS_EDI_DATA.
*  LS_EDI_DATA-SEGNAM = 'E1EDK17'.
*  LS_EDI_DATA-SDATA = LS_E1EDK17.
*  APPEND LS_EDI_DATA TO LT_EDI_DATA.

  "发票号码
  LS_E1EDK02-QUALF = '009'.
  LS_E1EDK02-BELNR = CS_JSD_HEAD-ZFPHM.
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDK02'.
  LS_EDI_DATA-SDATA = LS_E1EDK02.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.


  "开票日期
  LS_E1EDK03-IDDAT = '012'.
  LS_E1EDK03-DATUM = CS_JSD_HEAD-ZKPSJ.
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDK03'.
  LS_EDI_DATA-SDATA = LS_E1EDK03.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.

  LS_E1EDK04-MWSKZ = '001'.
  LS_E1EDK04-MWSBT = ABS( CS_JSD_HEAD-ZHJSE ).
  "LS_E1EDK04-MSATZ = LS_T007-MWSK1.  "tax code
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDK04'.
  LS_EDI_DATA-SDATA = LS_E1EDK04.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.

  LS_E1EDK14-QUALF = '011'.
  LS_E1EDK14-ORGID =  CS_JSD_HEAD-BUKRS.
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDK14'.
  LS_EDI_DATA-SDATA = LS_E1EDK14.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.


  LS_E1EDS01-SUMID = '011'.
  LS_E1EDS01-SUMME = ABS( CS_JSD_HEAD-ZHSJE ).
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDS01'.
  LS_EDI_DATA-SDATA = LS_E1EDS01.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.

  LS_E1EDS01-SUMID = '090'.
  LS_E1EDS01-SUMME = CS_JSD_HEAD-JSDDH.  "结算单单号
  CLEAR LS_EDI_DATA.
  LS_EDI_DATA-SEGNAM = 'E1EDS01'.
  LS_EDI_DATA-SDATA = LS_E1EDS01.
  APPEND LS_EDI_DATA TO LT_EDI_DATA.


  LS_EDI_CTL-MESTYP = 'INVOIC'.
  LS_EDI_CTL-IDOCTP = 'INVOIC02'.

  LS_EDI_CTL-DIRECT = '2'.
  LS_EDI_CTL-RCVPRT = 'LS'.    "接收方合作伙伴类型
  LS_EDI_CTL-SNDPRT = 'LS'.    "接收方的合作伙伴编号


  LS_EDI_CTL-RCVPOR = 'SAP' && SY-SYSID ."'SAPS4D'.    "接收者端口(SAP 系统，EDI 子系统)
  LS_EDI_CTL-RCVPRN = SY-SYSID && 'CLNT' && SY-MANDT. "'S4DCLNT200'.    "接收方的合作伙伴编号
  LS_EDI_CTL-SNDPOR = 'SAP' && SY-SYSID ."'SAPS4D'.    "接收方的合作伙伴编号
  LS_EDI_CTL-SNDPRN = SY-SYSID && 'CLNT' && SY-MANDT.  "接收方的合作伙伴编号

  CLEAR LV_ERROR.
  CLEAR LV_DOCNUM.
  CALL FUNCTION 'IDOC_WRITE_AND_START_INBOUND'
    EXPORTING
      I_EDIDC                       = LS_EDI_CTL
*     DO_COMMIT                     = 'X'
    IMPORTING
      DOCNUM                        = LV_DOCNUM
      ERROR_BEFORE_CALL_APPLICATION = LV_ERROR
    TABLES
      I_EDIDD                       = LT_EDI_DATA
    EXCEPTIONS
      IDOC_NOT_SAVED                = 1
      OTHERS                        = 2.
  IF SY-SUBRC <> 0 OR LV_ERROR IS NOT INITIAL OR LV_DOCNUM IS INITIAL.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = '结算单提交失败'(M21).
  ELSE.

    CS_JSD_HEAD-JSDZT = GC_JSDZT_SUBMITED.
    CS_JSD_HEAD-EDI_DOCNUM = LV_DOCNUM.
    CS_JSD_HEAD-CHNAM = SY-UNAME.
    CS_JSD_HEAD-CHDAT = SY-DATUM.
    CS_JSD_HEAD-CHTIM = SY-UZEIT.

    MODIFY ZFIT079_HEAD FROM CS_JSD_HEAD.
    COMMIT WORK AND WAIT.

    CS_MSG-TYPE = 'S'.
    CS_MSG-MESSAGE = '结算单已提交成功'(M20).

    SUBMIT /OTX/PF01_P_IDF_TRIGGER WITH P_RESULT = SPACE  AND RETURN.

  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FPJSD_ITEM_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_FPJSD_ITEM_DATA .
  REFRESH GT_ALV[].

  SELECT COUNT(*)
     FROM ZFIT079_HEAD
    WHERE JSDDH = ZFIT079_HEAD-JSDDH
      AND JSDLX = ZFIT079_HEAD-JSDLX.
  IF SY-SUBRC <> 0.
    RETURN.
  ENDIF.

  IF ZFIT079_HEAD-JSDLX = GC_JSDLX_PO.
    IF ZFIT079_HEAD-ZYWLX = GC_ZYWLX_CONSIGNMENT.
      SELECT
      A~*,
      A1~BUKRS,
      A1~LIFNR,
      B~MAKTX,
      C~NAME1 AS WERKS_NAME
      INTO TABLE @DATA(LT_ITEM_DB_MRKO)
      FROM ZFIT079_ITEM AS A
      JOIN RKWA AS A1
        ON A1~MBLNR = A~MBLNR
       AND A1~MJAHR = A~MJAHR
       AND A1~ZEILE = A~ZEILE

      LEFT JOIN MAKT AS B
        ON A~MATNR = B~MATNR
       AND B~SPRAS = @SY-LANGU
      LEFT JOIN T001W AS C
        ON A~WERKS = C~WERKS
     WHERE A~JSDDH = @ZFIT079_HEAD-JSDDH.

      LOOP AT LT_ITEM_DB_MRKO INTO DATA(LS_ITEM_DB_MRKO).
        CLEAR GT_ALV.
        MOVE-CORRESPONDING LS_ITEM_DB_MRKO-A TO GT_ALV.
        MOVE-CORRESPONDING LS_ITEM_DB_MRKO TO GT_ALV.

        GT_ALV-MATNR = |{ LS_ITEM_DB_MRKO-A-MATNR ALPHA = OUT }|.

        GT_ALV-SELECT = 'X'.
        GT_ALV-FRMDB = 'X'.
        APPEND GT_ALV.
      ENDLOOP.

    ELSE. "基于PO的
      SELECT
      A~*,
      A1~LIFNR,
      A1~BUKRS,
      B~BATXT,
      C~NAME1 AS WERKS_NAME
      INTO TABLE @DATA(LT_ITEM_DB_PO)
      FROM ZFIT079_ITEM AS A
      LEFT JOIN EKKO AS A1
        ON A~EBELN = A1~EBELN

      LEFT JOIN T161T AS B
        ON B~SPRAS = @SY-LANGU
       AND A~BSART = B~BSART
       AND B~BSTYP = 'F'  "采购订单

      LEFT JOIN T001W AS C
        ON A~WERKS = C~WERKS
     WHERE A~JSDDH = @ZFIT079_HEAD-JSDDH.

      LOOP AT LT_ITEM_DB_PO INTO DATA(LS_ITEM_DB_PO).
        CLEAR GT_ALV.
        MOVE-CORRESPONDING LS_ITEM_DB_PO-A TO GT_ALV.
        MOVE-CORRESPONDING LS_ITEM_DB_PO TO GT_ALV.

        GT_ALV-SELECT = 'X'.
        GT_ALV-FRMDB = 'X'.
        APPEND GT_ALV.
      ENDLOOP.
    ENDIF.

  ELSE."无PO
    SELECT
     A~*,
     B~TXT20 AS GL_HKONT_TXT,
     C~KTEXT AS GL_KOSTL_TXT
     INTO TABLE @DATA(LT_ITEM_DB_NPO)
     FROM ZFIT079_ITEM AS A

     LEFT JOIN SKAT AS B
       ON B~SPRAS = @SY-LANGU
      AND B~KTOPL = @GC_KTOPL_VAMA
      AND B~SAKNR = A~GL_HKONT

     LEFT JOIN CSKT AS C
       ON C~SPRAS = @SY-LANGU
      AND C~KOKRS = @GC_KOKRS_1000
      AND C~KOSTL = A~GL_KOSTL
      AND C~DATBI >= @SY-DATUM
    WHERE A~JSDDH = @ZFIT079_HEAD-JSDDH.

    LOOP AT LT_ITEM_DB_NPO INTO DATA(LS_ITEM_DB_NPO).
      CLEAR GT_ALV.
      MOVE-CORRESPONDING LS_ITEM_DB_NPO-A TO GT_ALV.
      MOVE-CORRESPONDING LS_ITEM_DB_NPO TO GT_ALV.

      GT_ALV-SELECT = 'X'.
      GT_ALV-FRMDB = 'X'.
      APPEND GT_ALV.
    ENDLOOP.

  ENDIF.

  "显示转换成外部
  LOOP AT GT_ALV.
    GT_ALV-EBELN = |{ GT_ALV-EBELN ALPHA = OUT }|.
    GT_ALV-MATNR = |{ GT_ALV-MATNR ALPHA = OUT }|.
    GT_ALV-CHARG = |{ GT_ALV-CHARG ALPHA = OUT }|.
    GT_ALV-MBLNR = |{ GT_ALV-MBLNR ALPHA = OUT }|.
    GT_ALV-SHEET_NO = |{ GT_ALV-SHEET_NO ALPHA = OUT }|.
    GT_ALV-GL_HKONT = |{ GT_ALV-GL_HKONT ALPHA = OUT }|.
    GT_ALV-GL_KOSTL = |{ GT_ALV-GL_KOSTL ALPHA = OUT }|.

    MODIFY GT_ALV.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_QUERY_CONSIGNMENT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_QUERY_CONSIGNMENT .
  DATA:
    LV_TABIX    TYPE I,
    LV_STR_TMP  TYPE STRING,
    LV_STR      TYPE STRING,
    LS_RKWA     TYPE RKWA,
    LS_RSPARAMS TYPE RSPARAMS,
    LV_TAXEXIST TYPE C,
    LT_RSPARAMS TYPE STANDARD TABLE OF RSPARAMS.

  PERFORM   FRM_GET_FPJSD_ITEM_DATA. "重新获取发票结算单行项目数据
  GT_ALV_DB[] = GT_ALV[].
  SORT GT_ALV_DB BY EBELN EBELP MBLNR MJAHR ZEILE.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'SO_BUKRS'.
  LS_RSPARAMS-KIND = 'S'.
  LS_RSPARAMS-SIGN = 'I'.
  LS_RSPARAMS-OPTION = 'EQ'.
  LS_RSPARAMS-LOW = ZFIT079_HEAD-BUKRS.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'SO_LIFNR'.
  LS_RSPARAMS-KIND = 'S'.
  LS_RSPARAMS-SIGN = 'I'.
  LS_RSPARAMS-OPTION = 'EQ'.
  LS_RSPARAMS-LOW = ZFIT079_HEAD-LIFNR.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  LOOP AT S_WERKS.
    CLEAR LS_RSPARAMS.
    LS_RSPARAMS-SELNAME = 'SO_WERKS'.
    LS_RSPARAMS-KIND = 'S'.
    LS_RSPARAMS-SIGN = S_WERKS-SIGN.
    LS_RSPARAMS-OPTION = S_WERKS-OPTION.
    LS_RSPARAMS-LOW = S_WERKS-LOW.
    LS_RSPARAMS-HIGH = S_WERKS-HIGH.
    APPEND LS_RSPARAMS TO LT_RSPARAMS.
  ENDLOOP.

  LOOP AT S_MATNR.
    CLEAR LS_RSPARAMS.
    LS_RSPARAMS-SELNAME = 'SO_MATNR'.
    LS_RSPARAMS-KIND = 'S'.
    LS_RSPARAMS-SIGN = S_MATNR-SIGN.
    LS_RSPARAMS-OPTION = S_MATNR-OPTION.
    LS_RSPARAMS-LOW = S_MATNR-LOW.
    LS_RSPARAMS-HIGH = S_MATNR-HIGH.
    APPEND LS_RSPARAMS TO LT_RSPARAMS.
  ENDLOOP.

  LOOP AT S_BELNR.
    CLEAR LS_RSPARAMS.
    LS_RSPARAMS-SELNAME = 'SO_MBLNR'.
    LS_RSPARAMS-KIND = 'S'.
    LS_RSPARAMS-SIGN = S_BELNR-SIGN.
    LS_RSPARAMS-OPTION = S_BELNR-OPTION.
    LS_RSPARAMS-LOW = S_BELNR-LOW.
    LS_RSPARAMS-HIGH = S_BELNR-HIGH.
    APPEND LS_RSPARAMS TO LT_RSPARAMS.
  ENDLOOP.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XKONS'.   "Consignment
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XPIPE'.   "Pipeline
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = SPACE.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XANZ'.    "Display
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XABR'.    "Settle
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = SPACE.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.


  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XNINV'.  "Withdrawals Not Settle
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XINV'.   "Settled Withdrawals
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = SPACE.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  DATA:
    LR_ALV_DATA    TYPE REF TO DATA,
    LT_MRKO_RESULT TYPE TABLE OF MRM_CON_ALV WITH HEADER LINE.

  FIELD-SYMBOLS : <LT_ALV_DATA> TYPE ANY TABLE .
  FIELD-SYMBOLS : <LT_DATA> TYPE ANY. "LIKE LINE OF  it_tab .

  CL_SALV_BS_RUNTIME_INFO=>SET( EXPORTING DISPLAY  = ABAP_FALSE
                                          METADATA = ABAP_FALSE
                                          DATA     = ABAP_TRUE ).

  SUBMIT RMVKON00 WITH SELECTION-TABLE LT_RSPARAMS
     LINE-SIZE SY-LINSZ
     EXPORTING LIST TO MEMORY
                  AND RETURN.

  TRY.
      CL_SALV_BS_RUNTIME_INFO=>GET_DATA_REF( IMPORTING R_DATA = LR_ALV_DATA ).
      ASSIGN LR_ALV_DATA->* TO <LT_ALV_DATA>.
    CATCH CX_SALV_BS_SC_RUNTIME_INFO.

  ENDTRY.
  CL_SALV_BS_RUNTIME_INFO=>CLEAR_ALL( ).

  IF <LT_ALV_DATA> IS NOT ASSIGNED.
    MESSAGE TEXT-M05 TYPE 'S'.
    GT_ALV[] = GT_ALV_DB[].
  ELSE.

    LT_MRKO_RESULT[] = <LT_ALV_DATA>.
    IF LT_MRKO_RESULT[] IS INITIAL.
      MESSAGE TEXT-M05 TYPE 'S'.
      GT_ALV[] = GT_ALV_DB[].

    ELSE.

      SELECT
        WERKS,
        NAME1
        INTO TABLE @DATA(LT_T001W)
        FROM T001W
         FOR ALL ENTRIES IN @LT_MRKO_RESULT
       WHERE WERKS = @LT_MRKO_RESULT-WERKS.

      SELECT
        MATNR,
        MAKTX
        INTO TABLE @DATA(LT_MAKT)
        FROM MAKT
         FOR ALL ENTRIES IN @LT_MRKO_RESULT
       WHERE MATNR = @LT_MRKO_RESULT-MATNR
         AND SPRAS = @SY-LANGU.

      SORT LT_T001W BY WERKS.
      SORT LT_MAKT BY MATNR.

      REFRESH GT_ALV[].

      LOOP AT LT_MRKO_RESULT .
        CLEAR GT_ALV.
        GT_ALV-MBLNR = LT_MRKO_RESULT-MBLNR.
        GT_ALV-MJAHR = LT_MRKO_RESULT-MJAHR.
        GT_ALV-ZEILE = LT_MRKO_RESULT-ZEILE.

        GT_ALV-BUKRS = LT_MRKO_RESULT-BUKRS.
        GT_ALV-LIFNR = LT_MRKO_RESULT-LIFNR.
        GT_ALV-WERKS = LT_MRKO_RESULT-WERKS.
        READ TABLE LT_T001W INTO DATA(LS_T001W)
          WITH KEY WERKS = GT_ALV-WERKS
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          GT_ALV-WERKS_NAME = LS_T001W-NAME1.
        ENDIF.

        GT_ALV-MATNR = |{ LT_MRKO_RESULT-MATNR  ALPHA = OUT }|.
        READ TABLE LT_MAKT INTO DATA(LS_MAKT)
          WITH KEY MATNR = LT_MRKO_RESULT-MATNR
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          GT_ALV-TXZ01 = LS_MAKT-MAKTX.
        ENDIF.

        GT_ALV-ERFMG = LT_MRKO_RESULT-BSTMG.
        GT_ALV-ERFME = LT_MRKO_RESULT-BSTME.
        GT_ALV-WRBTR = LT_MRKO_RESULT-WRBTR.

        "寄售取采购信息记录的税码
        SELECT SINGLE
          B~MWSKZ
          INTO GT_ALV-MWSKZ
          FROM EINA AS A
          JOIN EINE AS B
            ON A~INFNR = B~INFNR
         WHERE A~MATNR = LT_MRKO_RESULT-MATNR
           AND A~LIFNR = LT_MRKO_RESULT-LIFNR
           AND B~EKORG = 'CN10'
           AND B~WERKS = LT_MRKO_RESULT-WERKS
           AND B~ESOKZ = '2'. "Consignment
        IF GT_ALV-MWSKZ IS NOT INITIAL.
          PERFORM FRM_GET_TAX_RATE USING 'CN' GT_ALV-MWSKZ CHANGING GT_ALV-ZFPSL LV_TAXEXIST.
          GT_ALV-ZFPSE = GT_ALV-WRBTR * GT_ALV-ZFPSL / 100.
        ENDIF.

        "发票结算单数据库已经存在，以数据库里面的为准
        CLEAR GT_ALV_DB.
        READ TABLE GT_ALV_DB WITH KEY
          EBELN = GT_ALV-EBELN EBELP = GT_ALV-EBELP
          MBLNR = GT_ALV-MBLNR MJAHR = GT_ALV-MJAHR ZEILE = GT_ALV-ZEILE
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          LV_TABIX = SY-TABIX.

          GT_ALV-SELECT = 'X'.
          GT_ALV-FRMDB = 'X'.

          DELETE GT_ALV_DB INDEX LV_TABIX.
        ENDIF.

        APPEND GT_ALV.
      ENDLOOP.

      "在行项目表中，不在查询结果里的，附加到ALV中
      LOOP AT GT_ALV_DB.
        GT_ALV_DB-SELECT = 'X'.
        GT_ALV_DB-FRMDB = 'X'.
        MODIFY GT_ALV_DB.
      ENDLOOP.

      INSERT LINES OF GT_ALV_DB INTO GT_ALV INDEX 1.

    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_QUERY_PO
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_QUERY_PO .
  DATA:
    LV_TABIX           TYPE I,
    LT_R_BUKRS         TYPE TT_R_BUKRS WITH HEADER LINE,
    LT_R_LIFNR         TYPE TT_R_LIFNR WITH HEADER LINE,
    LT_R_CHARG         TYPE TT_R_CHARG WITH HEADER LINE,
    LT_GR_QUERY_RESULT TYPE TABLE OF TY_GR_QUERY_RESULT WITH HEADER LINE.

  PERFORM   FRM_GET_FPJSD_ITEM_DATA. "重新获取发票结算单行项目数据
  GT_ALV_DB[] = GT_ALV[].
  SORT GT_ALV_DB BY EBELN EBELP MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.

  REFRESH LT_R_BUKRS[].
  CLEAR LT_R_BUKRS.
  LT_R_BUKRS-SIGN = 'I'.
  LT_R_BUKRS-OPTION = 'EQ'.
  LT_R_BUKRS-LOW = ZFIT079_HEAD-BUKRS.
  APPEND LT_R_BUKRS.

  REFRESH LT_R_LIFNR[].
  CLEAR LT_R_LIFNR.
  LT_R_LIFNR-SIGN = 'I'.
  LT_R_LIFNR-OPTION = 'EQ'.
  LT_R_LIFNR-LOW = ZFIT079_HEAD-LIFNR.
  APPEND LT_R_LIFNR.

  IF S_CHARG[] IS NOT INITIAL OR S_LICHA[] IS NOT INITIAL.
    PERFORM FRM_ADD_CHARG USING S_CHARG[] S_LICHA[] CHANGING LT_R_CHARG[].
  ENDIF.

  CLEAR GV_POPUP_YES.
  IF S_BSART[] IS INITIAL AND S_EKGRP[] IS INITIAL AND S_EBELN[] IS INITIAL AND S_EBELP[] IS INITIAL AND
    S_EBENP[] IS INITIAL AND S_WERKS[] IS INITIAL AND S_MATNR[] IS INITIAL AND S_GJAHR[] IS INITIAL AND
    S_BELNR[] IS INITIAL AND LT_R_CHARG[] IS INITIAL AND S_ERNAM[] IS INITIAL AND S_AEDAT[] IS INITIAL.

    GV_POPUP_MSG = '您未输入任何收货行查询条件，确认查询？'(M38).
    CONDENSE GV_POPUP_MSG.
    PERFORM FRM_POPUP_TO_CONFIRM USING GV_POPUP_MSG CHANGING GV_POPUP_YES.

  ELSE.
    GV_POPUP_YES = 'X'.
  ENDIF.

  IF GV_POPUP_YES IS NOT INITIAL.
    IF ZFIT078_HEAD-INVOICE_LINE CS '17' "海关缴款书
      OR P_XBNKYF IS NOT INITIAL.  "运费
      REFRESH LT_R_LIFNR[].
    ENDIF.

    PERFORM FRM_GET_DATA_GR USING
      'X'       "排除结算单中存在的物料凭证
      ZFIT079_HEAD-JSDDH  "当前结算单
      ZFIT079_HEAD-WAERS  "当前结算单货币
      LT_R_BUKRS[]
      LT_R_LIFNR[]
      S_BSART[]
      S_EKGRP[]
      S_EBELN[]
      S_EBELP[]
      S_EBENP[]
      S_WERKS[]
      S_MATNR[]
      S_GJAHR[]
      S_BELNR[]
      LT_R_CHARG[]
      S_ERNAM[]
      S_AEDAT[]
      ZFIT079_HEAD-VORGANG
      P_XWARE  "货款/服务
      P_XBNKYF "运费
      P_XBNKQT "其它费用
      P_ZBL    "保留收货数量金额为零的建议行
    CHANGING LT_GR_QUERY_RESULT[] GS_MSG.

    IF GS_MSG-TYPE = 'E'.
      MESSAGE GS_MSG-MESSAGE TYPE 'S' .
      GT_ALV[] = GT_ALV_DB[].


    ELSE.

      IF GS_MSG-TYPE = 'W'.
        MESSAGE GS_MSG-MESSAGE TYPE 'W' .
      ENDIF.

      GT_ALV[] = LT_GR_QUERY_RESULT[].

      LOOP AT GT_ALV.
        "发票结算单数据库已经存在，以数据库里面的为准
        CLEAR GT_ALV_DB.
        READ TABLE GT_ALV_DB WITH KEY
          EBELN = GT_ALV-EBELN EBELP = GT_ALV-EBELP
          MBLNR = GT_ALV-MBLNR MJAHR = GT_ALV-MJAHR
          ZEILE = GT_ALV-ZEILE SHEET_NO = GT_ALV-SHEET_NO
          COND_TYPE = GT_ALV-COND_TYPE
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          LV_TABIX = SY-TABIX.

          GT_ALV-SELECT = 'X'.
          GT_ALV-FRMDB = 'X'.
          GT_ALV-WRBTR = GT_ALV_DB-WRBTR.
          GT_ALV-BEZNK = GT_ALV_DB-BEZNK.
          GT_ALV-MWSKZ = GT_ALV_DB-MWSKZ.
          GT_ALV-ZFPSL = GT_ALV_DB-ZFPSL.
          GT_ALV-ZFPSE = GT_ALV_DB-ZFPSE.

          DELETE GT_ALV_DB INDEX LV_TABIX.
        ENDIF.

        MODIFY GT_ALV.
      ENDLOOP.

      "在行项目表中，不在查询结果里的，附加到ALV中
      LOOP AT GT_ALV_DB.
        GT_ALV_DB-SELECT = 'X'.
        GT_ALV_DB-FRMDB = 'X'.
        MODIFY GT_ALV_DB.
      ENDLOOP.

      INSERT LINES OF GT_ALV_DB INTO GT_ALV INDEX 1.
    ENDIF.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_MRKO_POST
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> LT_JSD_ITEM
*&      <-- ZFIT079_HEAD
*&      <-- LS_MSG
*&---------------------------------------------------------------------*
FORM FRM_MRKO_POST  USING IT_JSD_ITEM TYPE TT_ZFIT079_ITEM
   CHANGING CS_JSD_HEAD TYPE ZFIT079_HEAD
            CS_MSG TYPE BAPIRET2.

  DATA:
    LV_STR_TMP     TYPE STRING,
    LV_STR         TYPE STRING,
    LS_RKWA        TYPE RKWA,
    LS_ZFIT078_HIS TYPE ZFIT078_HIS,
    LS_RSPARAMS    TYPE RSPARAMS,
    LT_RSPARAMS    TYPE STANDARD TABLE OF RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'SO_BUKRS'.
  LS_RSPARAMS-KIND = 'S'.
  LS_RSPARAMS-SIGN = 'I'.
  LS_RSPARAMS-OPTION = 'EQ'.
  LS_RSPARAMS-LOW = ZFIT079_HEAD-BUKRS.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.


  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'SO_LIFNR'.
  LS_RSPARAMS-KIND = 'S'.
  LS_RSPARAMS-SIGN = 'I'.
  LS_RSPARAMS-OPTION = 'EQ'.
  LS_RSPARAMS-LOW = ZFIT079_HEAD-LIFNR.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.


  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XKONS'.   "Consignment
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XPIPE'.   "Pipeline
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = SPACE.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XANZ'.    "Display
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = ''.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XABR'.    "Settle
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.


  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XNINV'.  "Withdrawals Not Settle
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = 'X'.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  CLEAR LS_RSPARAMS.
  LS_RSPARAMS-SELNAME = 'PA_XINV'.   "Display Settled Withdrawals
  LS_RSPARAMS-KIND = 'P'.
  LS_RSPARAMS-LOW = ''.
  APPEND LS_RSPARAMS TO LT_RSPARAMS.

  "填充物料凭证
  LOOP AT IT_JSD_ITEM INTO DATA(LS_JSD_ITEM).
    CLEAR LS_RSPARAMS.
    LS_RSPARAMS-SELNAME = 'SO_MBLNR'.
    LS_RSPARAMS-KIND = 'S'.
    LS_RSPARAMS-SIGN = 'I'.
    LS_RSPARAMS-OPTION = 'EQ'.
    LS_RSPARAMS-LOW = LS_JSD_ITEM-MBLNR.
    APPEND LS_RSPARAMS TO LT_RSPARAMS.
  ENDLOOP.

  DATA:
    LR_ALV_DATA    TYPE REF TO DATA,
    LT_MRKO_RESULT TYPE TABLE OF MRM_CON_ALV WITH HEADER LINE.

  FIELD-SYMBOLS : <LT_ALV_DATA> TYPE ANY TABLE .
  FIELD-SYMBOLS : <LT_DATA> TYPE ANY. "LIKE LINE OF  it_tab .

  CL_SALV_BS_RUNTIME_INFO=>SET( EXPORTING DISPLAY  = ABAP_FALSE
                                          METADATA = ABAP_FALSE
                                          DATA     = ABAP_TRUE ).

  SUBMIT RMVKON00 WITH SELECTION-TABLE LT_RSPARAMS
     LINE-SIZE SY-LINSZ
     EXPORTING LIST TO MEMORY
                  AND RETURN.

  TRY.
      CL_SALV_BS_RUNTIME_INFO=>GET_DATA_REF( IMPORTING R_DATA = LR_ALV_DATA ).
      ASSIGN LR_ALV_DATA->* TO <LT_ALV_DATA>.
    CATCH CX_SALV_BS_SC_RUNTIME_INFO.

  ENDTRY.
  CL_SALV_BS_RUNTIME_INFO=>CLEAR_ALL( ).

  IF <LT_ALV_DATA> IS NOT ASSIGNED.
    CS_MSG-TYPE = 'E'.
    CS_MSG-MESSAGE = '寄售过账出错，请重试'(M30).
  ELSE.

    LT_MRKO_RESULT[] = <LT_ALV_DATA>.
    IF LT_MRKO_RESULT[] IS INITIAL.
      CS_MSG-TYPE = 'E'.
      CS_MSG-MESSAGE = '寄售过账出错，请重试'(M30).
      RETURN.
    ELSE.
      LOOP AT LT_MRKO_RESULT WHERE BELNR IS NOT INITIAL.
        CS_JSD_HEAD-BELNR = LT_MRKO_RESULT-BELNR.
        CS_JSD_HEAD-GJAHR = LT_MRKO_RESULT-GJAHR.
        EXIT.
      ENDLOOP.
      IF SY-SUBRC = 0.

        CS_JSD_HEAD-JSDZT = GC_JSDZT_POSTED.
        CS_JSD_HEAD-CHNAM = SY-UNAME.
        CS_JSD_HEAD-CHDAT = SY-DATUM.
        CS_JSD_HEAD-CHTIM = SY-UZEIT.

        LS_ZFIT078_HIS-ZFPHM = CS_JSD_HEAD-ZFPHM.
        LS_ZFIT078_HIS-CHNAM = SY-UNAME.
        LS_ZFIT078_HIS-CHDAT = SY-DATUM.
        LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
        LS_ZFIT078_HIS-ZFPZT = '40'.  "已过帐
        LS_ZFIT078_HIS-JSDDH = CS_JSD_HEAD-JSDDH.
        LS_ZFIT078_HIS-BELNR = CS_JSD_HEAD-BELNR.
        LS_ZFIT078_HIS-GJAHR = CS_JSD_HEAD-GJAHR.

        MODIFY ZFIT079_HEAD FROM CS_JSD_HEAD.
        MODIFY ZFIT078_HIS FROM LS_ZFIT078_HIS.
        COMMIT WORK AND WAIT.

        CS_MSG-TYPE = 'S'.
        CS_MSG-MESSAGE = '结算单已过账成功'(M20).

      ELSE.

        LOOP AT LT_MRKO_RESULT WHERE TEXT IS NOT INITIAL.
          CS_MSG-TYPE = 'E'.
          CS_MSG-MESSAGE = LT_MRKO_RESULT-TEXT.
          EXIT.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SYNCFILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_SYNCFILE USING IS_ZFIT079_HEAD TYPE ZFIT079_HEAD "该Form ZFIU021也会调用
                        IV_NO_POPUP TYPE C.
  DATA:
    LS_MSG       TYPE BAPIRET2,
    LV_OBJECT_ID TYPE SAEOBJID,
    LT_CONN      TYPE TABLE OF TOAV0 WITH HEADER LINE,
    LT_TOAAT     TYPE STANDARD TABLE OF TOAAT,
    LS_MSG_DIS   TYPE ESP1_MESSAGE_WA_TYPE,
    LT_MSG_DIS   TYPE ESP1_MESSAGE_TAB_TYPE.

  DATA:
    LV_AR_OBJECT   TYPE TOAOM-AR_OBJECT,
    LV_FILE_NAME   TYPE CHAR255,
    LS_RETURN      TYPE BAPIRET2,
    LT_R_OBJECT_ID TYPE RANGE OF TOA01-OBJECT_ID WITH HEADER LINE.

  IF IS_ZFIT079_HEAD-JSDDH IS NOT INITIAL.
    SELECT *
      INTO TABLE @DATA(LT_ZFIT079_ITEM)
      FROM ZFIT079_ITEM
     WHERE JSDDH = @IS_ZFIT079_HEAD-JSDDH.

    SELECT *
      INTO TABLE @DATA(LT_ZFIT078_HEAD)
      FROM ZFIT078_HEAD
       FOR ALL ENTRIES IN @LT_ZFIT079_ITEM
     WHERE ZFPHM = @LT_ZFIT079_ITEM-ZYSFPHM.

    SELECT SINGLE *
      INTO @DATA(LS_ZFIU019_ATTACH)
      FROM ZFIU019_ATTACH
     WHERE LIFNR = @IS_ZFIT079_HEAD-LIFNR.

    SORT LT_ZFIT078_HEAD BY ZFPHM.

    "Get all existing connections
    LV_OBJECT_ID = IS_ZFIT079_HEAD-JSDDH.
    CALL FUNCTION 'ARCHIV_GET_CONNECTIONS'
      EXPORTING
        OBJECTTYPE      = GC_SAP_OBJECT
        OBJECT_ID       = LV_OBJECT_ID
      TABLES
        CONNECTIONS     = LT_CONN
        FILE_ATTRIBUTES = LT_TOAAT
      EXCEPTIONS
        NOTHING_FOUND   = 0
        OTHERS          = 0.

    "组合附件信息
    LOOP AT LT_CONN.
      READ TABLE LT_TOAAT INTO DATA(LS_TOAAT)
        WITH KEY ARC_DOC_ID = LT_CONN-ARC_DOC_ID.
      IF SY-SUBRC = 0.
        LT_CONN-OBJECT_ID = LS_TOAAT-FILENAME. "该字段放文件名
        MODIFY LT_CONN.
      ENDIF.
    ENDLOOP.

    DATA: LT_ZFIT079_ITEM_TMP LIKE LT_ZFIT079_ITEM.

    LT_ZFIT079_ITEM_TMP = LT_ZFIT079_ITEM.
    SORT LT_ZFIT079_ITEM_TMP BY ZYSFPHM.
    DELETE ADJACENT DUPLICATES FROM LT_ZFIT079_ITEM_TMP COMPARING ZYSFPHM.

    "发票PDF文件
    LOOP AT LT_ZFIT079_ITEM_TMP INTO DATA(LS_ITEM_TMP).

      LOOP AT LT_CONN WHERE AR_OBJECT = 'ZINV_PDF' AND OBJECT_ID CS LS_ITEM_TMP-ZYSFPHM.
        CLEAR LS_MSG_DIS.
        LS_MSG_DIS-MSGTY = 'S'.
        LS_MSG_DIS-MSGID = '00'.
        LS_MSG_DIS-MSGNO = '001'.
        LS_MSG_DIS-MSGV1 = '发票PDF附件已经上传'(M26).
        LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
        APPEND LS_MSG_DIS TO LT_MSG_DIS.
      ENDLOOP.
      IF SY-SUBRC <> 0.
        IF IS_ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_FOREIGN
               "草稿或者已提交状态才能同步附件
               AND ( IS_ZFIT079_HEAD-JSDZT = GC_JSDZT_DRAFT OR IS_ZFIT079_HEAD-JSDZT = GC_JSDZT_SUBMITED ).

          READ TABLE LT_ZFIT078_HEAD INTO DATA(LS_ZFIT078_HEAD)
            WITH KEY ZFPHM = LS_ITEM_TMP-ZYSFPHM
            BINARY SEARCH.
          IF SY-SUBRC = 0.

            IF LS_ZFIT078_HEAD-PDF_URL IS NOT INITIAL.
              CLEAR LS_MSG.
              CALL FUNCTION 'ZFIFM_INV_SYNCFILE'
                EXPORTING
                  IV_JSDDH    = IS_ZFIT079_HEAD-JSDDH
                  IV_ZYSFPHM  = LS_ZFIT078_HEAD-ZFPHM
                  IV_URL      = LS_ZFIT078_HEAD-PDF_URL
                  IV_DOC_TYPE = 'PDF'
                IMPORTING
                  ES_MSG      = LS_MSG.

              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = LS_MSG-TYPE.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = LS_MSG-MESSAGE.
              LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.

            ELSE.
              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = 'W'.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = '发票无PDF URL信息，无法同步该附件'(M28).
              LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.

    "发票XML文件
    LOOP AT LT_ZFIT079_ITEM_TMP INTO LS_ITEM_TMP.

      LOOP AT LT_CONN WHERE AR_OBJECT = 'ZINV_XML' AND OBJECT_ID CS LS_ITEM_TMP-ZYSFPHM.
        CLEAR LS_MSG_DIS.
        LS_MSG_DIS-MSGTY = 'S'.
        LS_MSG_DIS-MSGID = '00'.
        LS_MSG_DIS-MSGNO = '001'.
        LS_MSG_DIS-MSGV1 = '发票XML附件已经上传'(M27).
        LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
        APPEND LS_MSG_DIS TO LT_MSG_DIS.
      ENDLOOP.
      IF SY-SUBRC <> 0.
        IF IS_ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_FOREIGN
               "草稿或者已提交状态才能同步附件
               AND ( IS_ZFIT079_HEAD-JSDZT = GC_JSDZT_DRAFT OR IS_ZFIT079_HEAD-JSDZT = GC_JSDZT_SUBMITED ).

          READ TABLE LT_ZFIT078_HEAD INTO LS_ZFIT078_HEAD
            WITH KEY ZFPHM = LS_ITEM_TMP-ZYSFPHM
            BINARY SEARCH.
          IF SY-SUBRC = 0.

            IF LS_ZFIT078_HEAD-XML_URL IS NOT INITIAL.
              CLEAR LS_MSG.
              CALL FUNCTION 'ZFIFM_INV_SYNCFILE'
                EXPORTING
                  IV_JSDDH    = IS_ZFIT079_HEAD-JSDDH
                  IV_ZYSFPHM  = LS_ZFIT078_HEAD-ZFPHM
                  IV_URL      = LS_ZFIT078_HEAD-XML_URL
                  IV_DOC_TYPE = 'XML'
                IMPORTING
                  ES_MSG      = LS_MSG.

              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = LS_MSG-TYPE.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = LS_MSG-MESSAGE.
              LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.

            ELSE.
              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = 'W'.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = '发票无XML URL信息，无法同步该附件'(M29).
              LS_MSG_DIS-MSGV2 = '-' && LS_ITEM_TMP-ZYSFPHM.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.

    "合同附件
    IF LS_ZFIU019_ATTACH-HT_PC IS INITIAL.
      IF IS_ZFIT079_HEAD-JSDLX = GC_JSDLX_PO AND IS_ZFIT079_HEAD-ZYWLX <> GC_ZYWLX_CONSIGNMENT.

        LT_ZFIT079_ITEM_TMP = LT_ZFIT079_ITEM.
        SORT LT_ZFIT079_ITEM_TMP BY EBELN.
        DELETE ADJACENT DUPLICATES FROM LT_ZFIT079_ITEM_TMP COMPARING EBELN.

        REFRESH LT_R_OBJECT_ID[].
        LOOP AT LT_ZFIT079_ITEM_TMP INTO DATA(LS_ZFIT079_ITEM) WHERE EBELN IS NOT INITIAL.

          CLEAR LT_R_OBJECT_ID.
          LT_R_OBJECT_ID-SIGN = 'I'.
          LT_R_OBJECT_ID-OPTION = 'EQ'.
          LT_R_OBJECT_ID-LOW = LS_ZFIT079_ITEM-EBELN.
          APPEND LT_R_OBJECT_ID.
        ENDLOOP.

        IF LT_R_OBJECT_ID[] IS NOT INITIAL.
          SELECT *
              INTO TABLE @DATA(LT_TOA01)
              FROM TOA01
             WHERE SAP_OBJECT = 'BUS2012'
               AND OBJECT_ID IN @LT_R_OBJECT_ID
               AND ARCHIV_ID = 'Z2'
               AND AR_OBJECT = 'ZCTRCT_PDF'.

          SORT LT_TOA01 BY ARC_DOC_ID.
          DELETE ADJACENT DUPLICATES FROM LT_TOA01 COMPARING ARC_DOC_ID.

          LOOP AT LT_TOA01 INTO DATA(LS_TOA01).
            CLEAR LT_CONN.
            READ TABLE LT_CONN WITH KEY
              ARC_DOC_ID = LS_TOA01-ARC_DOC_ID AR_OBJECT = 'ZCTRCT_PDF'.
            IF SY-SUBRC = 0.

              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = 'S'.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = '合同附件已经成功上传-PDF'(M34).
              LS_MSG_DIS-MSGV2 = '-' && LT_CONN-OBJECT_ID.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.

            ELSE.

              LV_AR_OBJECT = 'ZCTRCT_PDF'.
              LV_OBJECT_ID = IS_ZFIT079_HEAD-JSDDH.
              LV_FILE_NAME = LS_TOA01-OBJECT_ID &&  '.' && 'PDF'.

              CLEAR LS_RETURN.
              CALL METHOD /OTX/PF00_CL_UTILITIES=>ARCHIVE_CONNECTION_INSERT "BOB-4005
                EXPORTING
                  PI_ARC_DOC_ID = LS_TOA01-ARC_DOC_ID
                  PI_AR_DATE    = SY-DATUM
                  PI_AR_OBJECT  = LV_AR_OBJECT   "归档文件类型，比如ZINV_PDF
                  PI_OBJECT_ID  = LV_OBJECT_ID   "业务单号
                  PI_SAP_OBJECT = GC_SAP_OBJECT  "'ZZFI_FPJSD'.
                  PI_DOC_TYPE   = 'PDF'    "附件类型，比如PDF、XML等
                  PI_FILENAME   = LV_FILE_NAME
                  PI_CREATOR    = SY-UNAME
                IMPORTING
                  PES_RETURN    = LS_RETURN.

              IF LS_RETURN IS NOT INITIAL.
                LS_RETURN-TYPE = 'E'.
              ELSE.
                LS_RETURN-TYPE = 'S'.
                LS_RETURN-MESSAGE = '合同附件已经上传-PDF'(M34).
              ENDIF.

              CLEAR LS_MSG_DIS.
              LS_MSG_DIS-MSGTY = LS_RETURN-TYPE.
              LS_MSG_DIS-MSGID = '00'.
              LS_MSG_DIS-MSGNO = '001'.
              LS_MSG_DIS-MSGV1 = LS_RETURN-MESSAGE.
              LS_MSG_DIS-MSGV2 = '-' && LS_TOA01-OBJECT_ID.
              APPEND LS_MSG_DIS TO LT_MSG_DIS.
            ENDIF.
          ENDLOOP.

        ENDIF.
      ENDIF.
    ENDIF.

    DATA:
      LV_SHEET_FLG TYPE C,
      LS_RSPARAMS  TYPE RSPARAMS,
      LT_RSPARAMS  TYPE STANDARD TABLE OF RSPARAMS.

    DATA:
      LV_ZAUTO_GEN_GR_PDF_VALUE TYPE /PTGWFI/Z_CONST-ZVALUE,
      LT_R_BSART                TYPE RANGE OF EKKO-BSART WITH HEADER LINE.

    "检查入库单附件
    IF LS_ZFIU019_ATTACH-RKD_PC IS INITIAL.
      IF IS_ZFIT079_HEAD-JSDLX = GC_JSDLX_PO.

        READ TABLE LT_CONN WITH KEY AR_OBJECT = 'ZGR_PDF'.
        IF SY-SUBRC = 0.
          CLEAR LS_MSG_DIS.
          LS_MSG_DIS-MSGTY = 'S'.
          LS_MSG_DIS-MSGID = '00'.
          LS_MSG_DIS-MSGNO = '001'.
          LS_MSG_DIS-MSGV1 = '入库/服务验收单附件已经上传'(M32).
          LS_MSG_DIS-MSGV2 = '-' && LT_CONN-OBJECT_ID.
          APPEND LS_MSG_DIS TO LT_MSG_DIS.

        ELSE.

          "取配置表里面自动生成入库验收单的PO类型
          CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
            EXPORTING
              CONSTNAME          = 'ZAUTO_GEN_GR_PDF'
              PRODUCTCODE        = '901'
            IMPORTING
              CONSTVALUE         = LV_ZAUTO_GEN_GR_PDF_VALUE
            EXCEPTIONS
              CONSTANT_NOT_FOUND = 1
              OTHERS             = 2.

          IF LV_ZAUTO_GEN_GR_PDF_VALUE IS NOT INITIAL.
            SPLIT LV_ZAUTO_GEN_GR_PDF_VALUE AT ',' INTO TABLE DATA(LT_VALUE).
            LOOP AT LT_VALUE INTO DATA(LS_VALUE).

              CONDENSE LS_VALUE.
              IF LS_VALUE IS NOT INITIAL.
                CLEAR LT_R_BSART.
                LT_R_BSART-SIGN = 'I'.
                LT_R_BSART-OPTION = 'EQ'.
                LT_R_BSART-LOW = LS_VALUE.
                APPEND LT_R_BSART.
              ENDIF.
            ENDLOOP.

            IF LT_R_BSART[] IS NOT INITIAL.
              LOOP AT LT_ZFIT079_ITEM INTO LS_ZFIT079_ITEM WHERE BSART IN LT_R_BSART
                AND ( MBLNR IS NOT INITIAL AND MJAHR IS NOT INITIAL ) OR SHEET_NO IS NOT INITIAL.

                CLEAR LS_RSPARAMS.
                LS_RSPARAMS-SELNAME = 'S_EBELN'.
                LS_RSPARAMS-KIND = 'S'.
                LS_RSPARAMS-SIGN = 'I'.
                LS_RSPARAMS-OPTION = 'EQ'.
                LS_RSPARAMS-LOW = LS_ZFIT079_ITEM-EBELN.
                APPEND LS_RSPARAMS TO LT_RSPARAMS.

                IF LS_ZFIT079_ITEM-MBLNR IS NOT INITIAL AND LS_ZFIT079_ITEM-MJAHR IS NOT INITIAL.
                  CLEAR LS_RSPARAMS.
                  LS_RSPARAMS-SELNAME = 'S_MBLNR'.
                  LS_RSPARAMS-KIND = 'S'.
                  LS_RSPARAMS-SIGN = 'I'.
                  LS_RSPARAMS-OPTION = 'EQ'.
                  LS_RSPARAMS-LOW = LS_ZFIT079_ITEM-MBLNR.
                  APPEND LS_RSPARAMS TO LT_RSPARAMS.

                  CLEAR LS_RSPARAMS.
                  LS_RSPARAMS-SELNAME = 'S_MJAHR'.
                  LS_RSPARAMS-KIND = 'S'.
                  LS_RSPARAMS-SIGN = 'I'.
                  LS_RSPARAMS-OPTION = 'EQ'.
                  LS_RSPARAMS-LOW = LS_ZFIT079_ITEM-MJAHR.
                  APPEND LS_RSPARAMS TO LT_RSPARAMS.

                ELSE.
                  LV_SHEET_FLG = 'X'.
                  CLEAR LS_RSPARAMS.
                  LS_RSPARAMS-SELNAME = 'S_LBLNI'.
                  LS_RSPARAMS-KIND = 'S'.
                  LS_RSPARAMS-SIGN = 'I'.
                  LS_RSPARAMS-OPTION = 'EQ'.
                  LS_RSPARAMS-LOW = LS_ZFIT079_ITEM-SHEET_NO.
                  APPEND LS_RSPARAMS TO LT_RSPARAMS.
                ENDIF.
              ENDLOOP.
              IF SY-SUBRC = 0.
                CLEAR LS_RSPARAMS.
                LS_RSPARAMS-SELNAME = 'P_INV'.
                LS_RSPARAMS-KIND = 'P'.
                LS_RSPARAMS-LOW = IS_ZFIT079_HEAD-JSDDH.
                APPEND LS_RSPARAMS TO LT_RSPARAMS.

                IF LV_SHEET_FLG IS NOT INITIAL.
                  SUBMIT ZMMF006D WITH SELECTION-TABLE LT_RSPARAMS
                     LINE-SIZE SY-LINSZ
                                  AND RETURN.

                ELSE.
                  SUBMIT ZMMF005D WITH SELECTION-TABLE LT_RSPARAMS
                    LINE-SIZE SY-LINSZ
                                 AND RETURN.
                ENDIF.
              ENDIF.

            ENDIF.
          ENDIF.
        ENDIF.



*        REFRESH LT_R_OBJECT_ID[].
*        LOOP AT LT_ZFIT079_ITEM INTO LS_ZFIT079_ITEM
*          WHERE MBLNR IS NOT INITIAL AND MJAHR IS NOT INITIAL.
*
*          CLEAR LT_R_OBJECT_ID.
*          LT_R_OBJECT_ID-SIGN = 'I'.
*          LT_R_OBJECT_ID-OPTION = 'EQ'.
*          LT_R_OBJECT_ID-LOW = LS_ZFIT079_ITEM-MBLNR && LS_ZFIT079_ITEM-MJAHR.
*          APPEND LT_R_OBJECT_ID.
*        ENDLOOP.
*
*
*        IF LT_R_OBJECT_ID IS NOT INITIAL.
*        REFRESH LT_TOA01[].
*        SELECT *
*          INTO TABLE @LT_TOA01
*          FROM TOA01
*         WHERE SAP_OBJECT = 'BUS2017'
*           AND OBJECT_ID IN @LT_R_OBJECT_ID
*           AND ARCHIV_ID = 'Z2'
*           AND AR_OBJECT = 'ZGR_PDF'.
*
*        SORT LT_TOA01 BY ARC_DOC_ID.
*        DELETE ADJACENT DUPLICATES FROM LT_TOA01 COMPARING ARC_DOC_ID.
*
*        LOOP AT LT_TOA01 INTO LS_TOA01.
*          CLEAR LT_CONN.
*          READ TABLE LT_CONN WITH KEY
*            ARC_DOC_ID = LS_TOA01-ARC_DOC_ID AR_OBJECT = 'ZGR_PDF'.
*          IF SY-SUBRC = 0.
*
*            CLEAR LS_MSG_DIS.
*            LS_MSG_DIS-MSGTY = 'S'.
*            LS_MSG_DIS-MSGID = '00'.
*            LS_MSG_DIS-MSGNO = '001'.
*            LS_MSG_DIS-MSGV1 = '入库单附件已经成功上传-PDF'(M32).
*            LS_MSG_DIS-MSGV2 = '-' && LT_CONN-OBJECT_ID.
*            APPEND LS_MSG_DIS TO LT_MSG_DIS.
*
*          ELSE.
*
*            LV_AR_OBJECT = 'ZGR_PDF'.
*            LV_OBJECT_ID = IS_ZFIT079_HEAD-JSDDH.
*            LV_FILE_NAME = LS_TOA01-OBJECT_ID &&  '.' && 'PDF'.
*
*            CLEAR LS_RETURN.
*            CALL METHOD /OTX/PF00_CL_UTILITIES=>ARCHIVE_CONNECTION_INSERT "BOB-4005
*              EXPORTING
*                PI_ARC_DOC_ID = LS_TOA01-ARC_DOC_ID
*                PI_AR_DATE    = SY-DATUM
*                PI_AR_OBJECT  = LV_AR_OBJECT   "归档文件类型，比如ZINV_PDF
*                PI_OBJECT_ID  = LV_OBJECT_ID   "业务单号
*                PI_SAP_OBJECT = GC_SAP_OBJECT  "'ZZFI_FPJSD'.
*                PI_DOC_TYPE   = 'PDF'    "附件类型，比如PDF、XML等
*                PI_FILENAME   = LV_FILE_NAME
*                PI_CREATOR    = SY-UNAME
*              IMPORTING
*                PES_RETURN    = LS_RETURN.
*
*            IF LS_RETURN IS NOT INITIAL.
*              LS_RETURN-TYPE = 'E'.
*            ELSE.
*              LS_RETURN-TYPE = 'S'.
*              LS_RETURN-MESSAGE = '入库单附件已经成功上传-PDF'(M32).
*            ENDIF.
*
*            CLEAR LS_MSG_DIS.
*            LS_MSG_DIS-MSGTY = LS_RETURN-TYPE.
*            LS_MSG_DIS-MSGID = '00'.
*            LS_MSG_DIS-MSGNO = '001'.
*            LS_MSG_DIS-MSGV1 = LS_RETURN-MESSAGE.
*            LS_MSG_DIS-MSGV2 = '-' && LS_TOA01-OBJECT_ID.
*            APPEND LS_MSG_DIS TO LT_MSG_DIS.
*          ENDIF.
*        ENDLOOP.
*      ENDIF.


      ENDIF.
    ENDIF.

    IF IV_NO_POPUP IS INITIAL AND LT_MSG_DIS[] IS NOT INITIAL.
      CALL FUNCTION 'C14Z_MESSAGES_SHOW_AS_POPUP'
        TABLES
          I_MESSAGE_TAB = LT_MSG_DIS.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_QUERY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DATA_QUERY .
  IF ZFIT079_HEAD-ZFPHM IS INITIAL.
    MESSAGE TEXT-M06 TYPE 'E'.
    RETURN.
  ENDIF.

  IF ZFIT079_HEAD-LIFNR IS INITIAL OR ZFIT079_HEAD-BUKRS IS INITIAL.
    MESSAGE TEXT-M07 TYPE 'E'.
    RETURN.
  ENDIF.

  IF ZFIT079_HEAD-ZYWLX = GC_ZYWLX_CONSIGNMENT.
    PERFORM FRM_DATA_QUERY_CONSIGNMENT.

  ELSE.
    PERFORM FRM_DATA_QUERY_PO.
  ENDIF.

  "显示转换成外部
  LOOP AT GT_ALV.
    GT_ALV-EBELN = |{ GT_ALV-EBELN ALPHA = OUT }|.
    GT_ALV-MATNR = |{ GT_ALV-MATNR ALPHA = OUT }|.
    GT_ALV-CHARG = |{ GT_ALV-CHARG ALPHA = OUT }|.
    GT_ALV-MBLNR = |{ GT_ALV-MBLNR ALPHA = OUT }|.
    GT_ALV-SHEET_NO = |{ GT_ALV-SHEET_NO ALPHA = OUT }|.
    GT_ALV-GL_HKONT = |{ GT_ALV-GL_HKONT ALPHA = OUT }|.
    GT_ALV-GL_KOSTL = |{ GT_ALV-GL_KOSTL ALPHA = OUT }|.

    MODIFY GT_ALV.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILTER_MAC_DOC
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- LT_EKBE
*&---------------------------------------------------------------------*
FORM FRM_FILTER_MAC_DOC
   USING IV_ZBL TYPE C          "保留收货数量金额为零的建议行
   CHANGING CT_EKBE TYPE TT_EKKO_EKPO.

  "取采购订单历史记录
  DATA:
    LV_TABIX    TYPE I,
    LV_MENGE_RE TYPE EKPO-MENGE,  "发票的数量
    LS_MSEG_CX  TYPE MSEG,
    LS_EKBE     TYPE TY_EKKO_EKPO,
    LS_EKBE_WE  TYPE TY_EKKO_EKPO,
    LT_MSEG_CX  TYPE STANDARD TABLE OF MSEG,
    LT_EKBE_TMP TYPE STANDARD TABLE OF TY_EKKO_EKPO,
    LT_EKBE_WE  TYPE STANDARD TABLE OF TY_EKKO_EKPO. "去掉冲销后的物料凭证

  DELETE CT_EKBE WHERE VGABE = '9'. "服务条目

  "排除物料凭证中冲销的物料凭证
  DELETE CT_EKBE WHERE BWART = '162'. "退货冲销
  DELETE CT_EKBE WHERE SHKZG = 'H' AND BWART <> '161'.  "普通冲销

  "获取冲销后的物料凭证
  LT_EKBE_TMP = CT_EKBE.
  DELETE LT_EKBE_TMP WHERE VGABE <> '1'.
  IF LT_EKBE_TMP IS NOT INITIAL.
    "取冲销的物料凭证
    SELECT *
      INTO TABLE LT_MSEG_CX
      FROM MSEG
      FOR ALL ENTRIES IN LT_EKBE_TMP
     WHERE EBELN = LT_EKBE_TMP-EBELN
       AND EBELP = LT_EKBE_TMP-EBELP
       AND SJAHR = LT_EKBE_TMP-GJAHR
       AND SMBLN = LT_EKBE_TMP-BELNR
       AND SMBLP = LT_EKBE_TMP-BUZEI.

    "根据批次获取退货订单数据 及 122部分冲销的物料凭证
    SELECT *
      INTO TABLE @DATA(LT_EKBE_DB_TH)
      FROM EKBE
       FOR ALL ENTRIES IN @LT_EKBE_TMP
     WHERE CHARG = @LT_EKBE_TMP-CHARG
       AND BWART = '122'. " ('161', '162', '122').

    "取发票/预制发票凭证
    SELECT *
         INTO TABLE @DATA(LT_EKBE_DB_FP)
         FROM EKBE
          FOR ALL ENTRIES IN @LT_EKBE_TMP
        WHERE VGABE IN ('2', 'P') "发票/预制发票
          AND EBELN = @LT_EKBE_TMP-EBELN
          AND EBELP = @LT_EKBE_TMP-EBELP
          AND LFGJA = @LT_EKBE_TMP-GJAHR
          AND LFBNR = @LT_EKBE_TMP-BELNR
          AND LFPOS = @LT_EKBE_TMP-BUZEI.

  ENDIF.

  "取出过滤冲销后的物料凭证
  LT_EKBE_TMP = CT_EKBE.
  LOOP AT LT_EKBE_TMP INTO LS_EKBE WHERE VGABE = '1'.
    READ TABLE LT_MSEG_CX TRANSPORTING NO FIELDS
       WITH KEY EBELN = LS_EKBE-EBELN
          EBELP = LS_EKBE-EBELP
          SJAHR = LS_EKBE-GJAHR
          SMBLN = LS_EKBE-BELNR
          SMBLP = LS_EKBE-BUZEI.
    IF SY-SUBRC <> 0.
      "排除122部分冲销的物料凭证
      LOOP AT LT_EKBE_DB_TH INTO DATA(LS_EKBE_DB_TH) WHERE BELNR <> LS_EKBE-BELNR AND CHARG = LS_EKBE-CHARG.
        IF LS_EKBE_DB_TH-SHKZG = 'H'.
          LS_EKBE-MENGE = LS_EKBE-MENGE - LS_EKBE_DB_TH-MENGE.
        ELSE.
          LS_EKBE-MENGE = LS_EKBE-MENGE + LS_EKBE_DB_TH-MENGE.
        ENDIF.
      ENDLOOP.
      IF SY-SUBRC = 0.
        IF LS_EKBE-MENGE > 0.
          APPEND LS_EKBE TO LT_EKBE_WE.
        ENDIF.
      ELSE.
        APPEND LS_EKBE TO LT_EKBE_WE.
      ENDIF.
    ENDIF.
  ENDLOOP.

  "发票检查
  IF IV_ZBL IS INITIAL. "保留收货建议行勾选 则不过滤已经做过发票的物料凭证

    LT_EKBE_TMP = CT_EKBE.
    LOOP AT LT_EKBE_WE INTO LS_EKBE_WE.
      LV_TABIX = SY-TABIX.

      CLEAR LV_MENGE_RE.
      LOOP AT LT_EKBE_DB_FP INTO DATA(LS_EKBE_DB_FP) WHERE EBELN = LS_EKBE_WE-EBELN AND EBELP = LS_EKBE_WE-EBELP
        AND LFGJA = LS_EKBE_WE-GJAHR AND LFBNR = LS_EKBE_WE-BELNR AND LFPOS = LS_EKBE_WE-BUZEI.

        IF LS_EKBE_DB_FP-SHKZG = 'S'.
          LV_MENGE_RE = LV_MENGE_RE + LS_EKBE_DB_FP-MENGE.
        ELSEIF LS_EKBE_DB_FP-SHKZG = 'H'.
          LV_MENGE_RE = LV_MENGE_RE - LS_EKBE_DB_FP-MENGE.
        ENDIF.
      ENDLOOP.

      IF ABS( LV_MENGE_RE ) >= LS_EKBE_WE-MENGE. "完全做过发票的GR行项目删除
        DELETE LT_EKBE_WE INDEX LV_TABIX.
      ENDIF.
    ENDLOOP.
  ENDIF.

  REFRESH CT_EKBE.
  CT_EKBE[] = LT_EKBE_WE[].
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ATTACH_CHECK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_ATTACH_CHECK .
  DATA:
    LS_MSG                       TYPE BAPIRET2,
    LV_CHECK_ALL                 TYPE C, "合同、发票、入库验收单
    LV_CHECK_INV_GR              TYPE C, "发票、入库验收单
    LV_OBJECT_ID                 TYPE SAEOBJID,
    LT_CONN                      TYPE TABLE OF TOAV0 WITH HEADER LINE,
    LT_TOAAT                     TYPE STANDARD TABLE OF TOAAT,
    LS_MSG_DIS                   TYPE ESP1_MESSAGE_WA_TYPE,
    LT_MSG_DIS                   TYPE ESP1_MESSAGE_TAB_TYPE,
    LV_XML_RE_FLG                TYPE C,
    LV_ZINVOICELINE_XML_RE_VALUE TYPE /PTGWFI/Z_CONST-ZVALUE,
    LT_R_INVLINE_XML_RE          TYPE RANGE OF STRING WITH HEADER LINE.

  SELECT  *
    INTO TABLE @DATA(LT_ZFIT079_ITEM)
    FROM ZFIT079_ITEM
   WHERE JSDDH = @ZFIT079_HEAD-JSDDH.

  SELECT *
    INTO TABLE @DATA(LT_ZFIT078_HEAD)
    FROM ZFIT078_HEAD
     FOR ALL ENTRIES IN @LT_ZFIT079_ITEM
   WHERE ZFPHM = @LT_ZFIT079_ITEM-ZYSFPHM.

  "Get all existing connections
  LV_OBJECT_ID = ZFIT079_HEAD-JSDDH.
  CALL FUNCTION 'ARCHIV_GET_CONNECTIONS'
    EXPORTING
      OBJECTTYPE      = GC_SAP_OBJECT
      OBJECT_ID       = LV_OBJECT_ID
    TABLES
      CONNECTIONS     = LT_CONN
      FILE_ATTRIBUTES = LT_TOAAT
    EXCEPTIONS
      NOTHING_FOUND   = 0
      OTHERS          = 0.

  "取配置表里面，必须XML发票附件检查的发票类型
  CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
    EXPORTING
      CONSTNAME          = 'ZINVOICELINE_XML_RE'
      PRODUCTCODE        = '901'
    IMPORTING
      CONSTVALUE         = LV_ZINVOICELINE_XML_RE_VALUE
    EXCEPTIONS
      CONSTANT_NOT_FOUND = 1
      OTHERS             = 2.

  IF LV_ZINVOICELINE_XML_RE_VALUE IS NOT INITIAL.
    SPLIT LV_ZINVOICELINE_XML_RE_VALUE AT ',' INTO TABLE DATA(LT_VALUE).
    LOOP AT LT_VALUE INTO DATA(LS_VALUE).
      CONDENSE LS_VALUE.
      IF LS_VALUE IS NOT INITIAL.

        CLEAR LT_R_INVLINE_XML_RE.
        LT_R_INVLINE_XML_RE-SIGN = 'I'.
        LT_R_INVLINE_XML_RE-OPTION = 'EQ'.
        LT_R_INVLINE_XML_RE-LOW = LS_VALUE.
        APPEND LT_R_INVLINE_XML_RE.
      ENDIF.
    ENDLOOP.
  ENDIF.

  LOOP AT LT_ZFIT078_HEAD TRANSPORTING NO FIELDS WHERE INVOICE_LINE+0(2) IN LT_R_INVLINE_XML_RE.
    LV_XML_RE_FLG = 'X'.
    EXIT.
  ENDLOOP.

  IF ZFIT078_HEAD IS INITIAL AND LT_ZFIT078_HEAD IS NOT INITIAL.
    READ TABLE LT_ZFIT078_HEAD INTO ZFIT078_HEAD INDEX 1.
  ENDIF.

  IF LV_XML_RE_FLG IS INITIAL.
    IF ZFIT078_HEAD-INVOICE_LINE+0(2) IN LT_R_INVLINE_XML_RE.
      LV_XML_RE_FLG = 'X'.
    ENDIF.
  ENDIF.

  IF LV_XML_RE_FLG IS NOT INITIAL AND ZFIT078_HEAD-INVOICE_LINE NS '17'. "海关缴款书
    READ TABLE LT_CONN WITH KEY AR_OBJECT = 'ZINV_XML' TRANSPORTING NO FIELDS.
    IF SY-SUBRC <> 0.
      CLEAR LS_MSG_DIS.
      LS_MSG_DIS-MSGTY = 'E'.
      LS_MSG_DIS-MSGID = '00'.
      LS_MSG_DIS-MSGNO = '001'.
      LS_MSG_DIS-MSGV1 = '请上传发票原始XML文件'.
      APPEND LS_MSG_DIS TO LT_MSG_DIS.
    ENDIF.
  ENDIF.

  IF ZFIT079_HEAD-JSDLX = GC_JSDLX_PO.
    LOOP AT LT_ZFIT079_ITEM INTO DATA(LS_ZFIT079_ITEM).
      CASE LS_ZFIT079_ITEM-BSART.
        WHEN 'NB' OR 'Z4' OR 'Z13' OR 'Z12'.
          LV_CHECK_ALL = 'X'.
          EXIT.

        WHEN 'Z3' OR 'Z98' OR 'Z5'.
          LV_CHECK_INV_GR = 'X'.
        WHEN OTHERS.
      ENDCASE.
    ENDLOOP.

    IF LV_CHECK_ALL IS NOT INITIAL OR LV_CHECK_INV_GR IS NOT INITIAL.
      IF LV_CHECK_ALL IS NOT INITIAL.
        READ TABLE LT_CONN WITH KEY AR_OBJECT = 'ZCTRCT_PDF' TRANSPORTING NO FIELDS.
        IF SY-SUBRC <> 0.
          CLEAR LS_MSG_DIS.
          LS_MSG_DIS-MSGTY = 'E'.
          LS_MSG_DIS-MSGID = '00'.
          LS_MSG_DIS-MSGNO = '001'.
          LS_MSG_DIS-MSGV1 = '请上传合同PDF文件'.
          APPEND LS_MSG_DIS TO LT_MSG_DIS.
        ENDIF.

        READ TABLE LT_CONN WITH KEY AR_OBJECT = 'ZGR_PDF' TRANSPORTING NO FIELDS.
        IF SY-SUBRC <> 0.
          CLEAR LS_MSG_DIS.
          LS_MSG_DIS-MSGTY = 'E'.
          LS_MSG_DIS-MSGID = '00'.
          LS_MSG_DIS-MSGNO = '001'.
          LS_MSG_DIS-MSGV1 = '请上传入库/服务验收单PDF文件'.
          APPEND LS_MSG_DIS TO LT_MSG_DIS.
        ENDIF.

      ELSEIF LV_CHECK_INV_GR IS NOT INITIAL.
        READ TABLE LT_CONN WITH KEY AR_OBJECT = 'ZINV_GEN' TRANSPORTING NO FIELDS.
        IF SY-SUBRC <> 0.
          CLEAR LS_MSG_DIS.
          LS_MSG_DIS-MSGTY = 'E'.
          LS_MSG_DIS-MSGID = '00'.
          LS_MSG_DIS-MSGNO = '001'.
          LS_MSG_DIS-MSGV1 = '请上传签收单到[通用格式文件]'.
          APPEND LS_MSG_DIS TO LT_MSG_DIS.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.

  IF LT_MSG_DIS[] IS NOT INITIAL.
    CALL FUNCTION 'C14Z_MESSAGES_SHOW_AS_POPUP'
      TABLES
        I_MESSAGE_TAB = LT_MSG_DIS.

    MESSAGE '请上传相关附件' TYPE 'E'.
  ENDIF.
ENDFORM.
